Merge branch 'master' of github.com:couchrest/couchrest_model

Conflicts:
	history.txt
This commit is contained in:
Sam Lown 2011-03-13 19:53:52 +01:00
commit 0e51dcfb9a
4 changed files with 18 additions and 2 deletions

2
.gitignore vendored
View file

@ -2,7 +2,7 @@
html/* html/*
pkg pkg
*.swp *.swp
.rvmrc .rvmrc*
.bundle .bundle
couchdb.std* couchdb.std*
*.*~ *.*~

View file

@ -9,6 +9,7 @@
* Narrow the rescued exception to avoid catching class evaluation errors that has nothing to to with the association (thanks Simone Carletti) * 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) * Fix validate uniqueness test that was never executed (thanks Simone Carletti)
* Adds a #reload method to reload document attributes (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)
* CollectionProxy no longer provided by default with simple views (pending deprication) * CollectionProxy no longer provided by default with simple views (pending deprication)
== CouchRest Model 1.0.0 == CouchRest Model 1.0.0

View file

@ -79,7 +79,7 @@ module CouchRest
# Match numeric string # Match numeric string
def typecast_to_numeric(value, method) def typecast_to_numeric(value, method)
if value.respond_to?(:to_str) 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) $1.send(method)
else else
value value

View file

@ -340,6 +340,11 @@ describe "Model properties" do
@course.estimate.should eql(1232434123.323) @course.estimate.should eql(1232434123.323)
end 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| [ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do it "does not typecast non-numeric value #{value.inspect}" do
@course.estimate = value @course.estimate = value
@ -426,6 +431,11 @@ describe "Model properties" do
@course['hours'].should eql(-24) @course['hours'].should eql(-24)
end 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| [ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do it "does not typecast non-numeric value #{value.inspect}" do
@course.hours = value @course.hours = value
@ -511,6 +521,11 @@ describe "Model properties" do
@course['profit'].should eql(BigDecimal('-24.35')) @course['profit'].should eql(BigDecimal('-24.35'))
end 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| [ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |value|
it "does not typecast non-numeric value #{value.inspect}" do it "does not typecast non-numeric value #{value.inspect}" do
@course.profit = value @course.profit = value