Browse files

support for big decimal properties

  • Loading branch information...
1 parent 74445d0 commit 66d6eb69c5fd5365137f82a1e5b9a46ecbe2896e @froderik froderik 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
View
2 lib/couch_potato/persistence/type_caster.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
8 spec/property_spec.rb
@@ -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
6 spec/spec_helper.rb
@@ -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 66d6eb6

Please sign in to comment.