Skip to content

Commit

Permalink
Merge pull request #87 from wrshawn/float_fix
Browse files Browse the repository at this point in the history
makes cast_native cast floats with no leading digits.
  • Loading branch information
langalex committed Jan 24, 2014
2 parents 5deeaf0 + ae53e2c commit 41c8edd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/couch_potato/persistence/type_caster.rb
@@ -1,7 +1,7 @@
module CouchPotato
module Persistence
class TypeCaster #:nodoc:
NUMBER_REGEX = /-?\d+\.?\d*/
NUMBER_REGEX = /-?\d*\.?\d*/

def cast(value, type)
if type == :boolean
Expand Down
15 changes: 15 additions & 0 deletions spec/property_spec.rb
Expand Up @@ -12,6 +12,7 @@ class Watch
property :custom_address, :type => [Address]
property :overwritten_read
property :overwritten_write
property :diameter, :type => Float

def overwritten_read
super.to_s
Expand Down Expand Up @@ -73,6 +74,20 @@ class SportsWatch < Watch
c.title.should == 3
end

it "should persist a float with leading digits" do
w = Watch.new :diameter => "46.5"
CouchPotato.database.save_document! w
w = CouchPotato.database.load_document w.id
w.diameter.should == 46.5
end

it "should persist a float with no leading digits" do
w = Watch.new :diameter => ".465"
CouchPotato.database.save_document! w
w = CouchPotato.database.load_document w.id
w.diameter.should == 0.465
end

it "should persist a big decimal" do
require 'bigdecimal'
c = BigDecimalContainer.new :number => BigDecimal.new( '42.42' )
Expand Down

0 comments on commit 41c8edd

Please sign in to comment.