Numeric types can be casted from strings with leading or trailing whitespace
Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
parent
53b052f631
commit
755a0c813f
2 changed files with 16 additions and 1 deletions
|
@ -79,7 +79,7 @@ module CouchRest
|
|||
# Match numeric string
|
||||
def typecast_to_numeric(value, method)
|
||||
if value.respond_to?(:to_str)
|
||||
if value.gsub(/,/, '.').gsub(/\.(?!\d*\Z)/, '').to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
|
||||
if value.strip.gsub(/,/, '.').gsub(/\.(?!\d*\Z)/, '').to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
|
||||
$1.send(method)
|
||||
else
|
||||
value
|
||||
|
|
|
@ -340,6 +340,11 @@ describe "Model properties" do
|
|||
@course.estimate.should eql(1232434123.323)
|
||||
end
|
||||
|
||||
it "should handle numbers with whitespace" do
|
||||
@course.estimate = " 24.35 "
|
||||
@course.estimate.should eql(24.35)
|
||||
end
|
||||
|
||||
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
|
||||
it "does not typecast non-numeric value #{value.inspect}" do
|
||||
@course.estimate = value
|
||||
|
@ -426,6 +431,11 @@ describe "Model properties" do
|
|||
@course['hours'].should eql(-24)
|
||||
end
|
||||
|
||||
it "should handle numbers with whitespace" do
|
||||
@course.hours = " 24 "
|
||||
@course['hours'].should eql(24)
|
||||
end
|
||||
|
||||
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
|
||||
it "does not typecast non-numeric value #{value.inspect}" do
|
||||
@course.hours = value
|
||||
|
@ -511,6 +521,11 @@ describe "Model properties" do
|
|||
@course['profit'].should eql(BigDecimal('-24.35'))
|
||||
end
|
||||
|
||||
it "should handle numbers with whitespace" do
|
||||
@course.profit = " 24.35 "
|
||||
@course['profit'].should eql(BigDecimal('24.35'))
|
||||
end
|
||||
|
||||
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
|
||||
it "does not typecast non-numeric value #{value.inspect}" do
|
||||
@course.profit = value
|
||||
|
|
Loading…
Reference in a new issue