Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for BigDecimal #74

Merged
merged 1 commit into from

2 participants

@froderik

Floats are not the best when it comes to arithmetic operations so therefore I added BigDecimal support along with a test. Now you can use BigDecimal as the type of a property:
property :number, :type => BigDecimal

@langalex
Owner

thanks great. i had monkeypatched this into a project but never got around to adding it to couch potato.

@langalex langalex merged commit e08ad14 into langalex:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 26, 2012
  1. @froderik
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.