Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Allow number string to not require leading digits before decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed Jun 23, 2009
1 parent 9f98e82 commit 7973cc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/property.rb
Expand Up @@ -1011,7 +1011,7 @@ def typecast_to_float(value)
# @api private
def typecast_to_numeric(value, method)
if value.respond_to?(:to_str)
if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?)\z/
if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
$1.send(method)
else
value
Expand Down
30 changes: 27 additions & 3 deletions spec/public/property_spec.rb
Expand Up @@ -358,6 +358,14 @@ class ::Image
@property.typecast('-24.35').should eql(-24.35)
end

it 'returns float representation of a zero string float, with no leading digits' do
@property.typecast('.0').should eql(0.0)
end

it 'returns float representation of a positive string float, with no leading digits' do
@property.typecast('.41').should eql(0.41)
end

it 'returns float representation of a zero integer' do
@property.typecast(0).should eql(0.0)
end
Expand All @@ -382,7 +390,7 @@ class ::Image
@property.typecast(BigDecimal('-24.35')).should eql(-24.35)
end

[ Object.new, true, '00.0', '.0', '0.', 'string' ].each do |value|
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do
@property.typecast(value).should equal(value)
end
Expand Down Expand Up @@ -423,6 +431,14 @@ class ::Image
@property.typecast('-24.35').should eql(-24)
end

it 'returns integer representation of a zero string float, with no leading digits' do
@property.typecast('.0').should eql(0)
end

it 'returns integer representation of a positive string float, with no leading digits' do
@property.typecast('.41').should eql(0)
end

it 'returns integer representation of a zero float' do
@property.typecast(0.0).should eql(0)
end
Expand All @@ -447,7 +463,7 @@ class ::Image
@property.typecast(BigDecimal('-24.35')).should eql(-24)
end

[ Object.new, true, '00.0', '.0', '0.', 'string' ].each do |value|
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do
@property.typecast(value).should equal(value)
end
Expand Down Expand Up @@ -488,6 +504,14 @@ class ::Image
@property.typecast('-24.35').should eql(BigDecimal('-24.35'))
end

it 'returns decimal representation of a zero string float, with no leading digits' do
@property.typecast('.0').should eql(BigDecimal('0.0'))
end

it 'returns decimal representation of a positive string float, with no leading digits' do
@property.typecast('.41').should eql(BigDecimal('0.41'))
end

it 'returns decimal representation of a zero integer' do
@property.typecast(0).should eql(BigDecimal('0.0'))
end
Expand All @@ -512,7 +536,7 @@ class ::Image
@property.typecast(-24.35).should eql(BigDecimal('-24.35'))
end

[ Object.new, true, '00.0', '.0', '0.', 'string' ].each do |value|
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do
@property.typecast(value).should equal(value)
end
Expand Down

0 comments on commit 7973cc5

Please sign in to comment.