Permalink
Browse files

Merge pull request #74 from froderik/master

Support for BigDecimal
  • Loading branch information...
2 parents 74445d0 + 66d6eb6 commit e08ad144eb0a900f4a35c4a55b0adea653a33b16 @langalex committed Nov 26, 2012
Showing with 16 additions and 0 deletions.
  1. +2 −0 lib/couch_potato/persistence/type_caster.rb
  2. +8 −0 spec/property_spec.rb
  3. +6 −0 spec/spec_helper.rb
@@ -32,6 +32,8 @@ def cast_native(value, type)
BigDecimal.new(value.to_s.scan(NUMBER_REGEX).join).round unless value.blank?
elsif type == Float
value.to_s.scan(NUMBER_REGEX).join.to_f unless value.blank?
+ elsif type == BigDecimal
+ BigDecimal.new(value.to_s) unless value.blank?
else
type.json_create value unless value.blank?
end
View
@@ -57,6 +57,14 @@ class CuckooClock < Watch
c.title.should == 3
end
+ it "should persist a big decimal" do
+ require 'bigdecimal'
+ c = BigDecimalContainer.new :number => BigDecimal.new( '42.42' )
+ CouchPotato.database.save_document! c
+ c = CouchPotato.database.load_document c.id
+ c.number.should == BigDecimal.new( '42.42' )
+ end
+
it "should persist a hash" do
c = Comment.new :title => {'key' => 'value'}
CouchPotato.database.save_document! c
View
@@ -31,6 +31,12 @@ class Comment
property :title
end
+class BigDecimalContainer
+ include CouchPotato::Persistence
+
+ property :number, :type => BigDecimal
+end
+
def recreate_db
CouchPotato.couchrest_database.recreate!
end

0 comments on commit e08ad14

Please sign in to comment.