From 755a0c813f4c000c29f6e79370f81162ff8052f6 Mon Sep 17 00:00:00 2001 From: Christopher Durtschi Date: Sat, 26 Feb 2011 01:33:16 -0700 Subject: [PATCH 1/3] Numeric types can be casted from strings with leading or trailing whitespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcos Tapajós --- lib/couchrest/model/typecast.rb | 2 +- spec/couchrest/property_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/model/typecast.rb b/lib/couchrest/model/typecast.rb index 45a3114..6e9499a 100644 --- a/lib/couchrest/model/typecast.rb +++ b/lib/couchrest/model/typecast.rb @@ -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 diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index 2c4eab0..1e3548d 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -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 From a37ca4c37b7f17c8572e106baea800a970e0b0e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Tue, 1 Mar 2011 22:54:16 -0300 Subject: [PATCH 2/3] Updating Readme --- history.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/history.txt b/history.txt index 7ce64d8..5aaa928 100644 --- a/history.txt +++ b/history.txt @@ -5,6 +5,7 @@ * Narrow the rescued exception to avoid catching class evaluation errors that has nothing to to with the association (thanks Simone Carletti) * Fix validate uniqueness test that was never executed (thanks Simone Carletti) * Adds a #reload method to reload document attributes (thanks Simone Carletti) + * Numeric types can be casted from strings with leading or trailing whitespace (thanks chrisdurtschi) == CouchRest Model 1.0.0 From 498ebf1599c7c0d6d5f855bd1b4a06ec496e9d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Tue, 1 Mar 2011 23:30:06 -0300 Subject: [PATCH 3/3] I'm using a shell script to run all specs in Ruby 1.9.2, MRI 1.8.7 and ree 1.8.7 and I need this change to help me. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5b1ad4c..b0683b0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ html/* pkg *.swp -.rvmrc +.rvmrc* .bundle couchdb.std* *.*~