-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID-5212 Support for Decimal128 type #5125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
aad61b9
5774fcb
4e0f500
ee97bae
dfa3beb
781ee19
fff7de5
f78de1c
778175b
3e88a37
392db16
99fac6f
ae3e12e
0ed2f21
5587d31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source "https://rubygems.org" | ||
gemspec path: '..' | ||
|
||
gem 'bson', '4.14.0' | ||
# This configuration doesn't require a specific driver version. When bson-4.14.0 | ||
# was released, current driver version was 2.17.0 | ||
gem 'mongo', '2.17.0' | ||
|
||
require_relative './standard' | ||
|
||
standard_dependencies |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,11 @@ def __to_inc__ | |
# | ||
# @return [ Object ] The object. | ||
def mongoize | ||
to_s | ||
if Mongoid.map_big_decimal_to_decimal128 | ||
BSON::Decimal128.new(self) | ||
else | ||
to_s | ||
end | ||
end | ||
|
||
# Is the BigDecimal a number? | ||
|
@@ -46,19 +50,40 @@ module ClassMethods | |
# | ||
# @return [ BigDecimal, nil ] A BigDecimal derived from the object or nil. | ||
def demongoize(object) | ||
object && object.numeric? ? BigDecimal(object.to_s) : nil | ||
unless object.nil? | ||
if object.is_a?(BSON::Decimal128) | ||
object.to_big_decimal | ||
elsif object.numeric? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like the Test with a time field:
produces:
Test with the code as proposed with BigDecimal field:
produces:
I think it would be better to always attempt to construct a BigDecimal, allowing that call to raise whatever exceptions on problems. @comandeo thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree. I think demongoize should return nil unless the object can be represented as a BigDecimal. Should check the behavior of demongoize on other classes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @p-mongo I kind of agree with @johnnyshields here... Here are some examples of invalid values to demongoize:
Now, I'm not sure why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @neilshweky is correct should make this return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually after rereading your comment, I agree @p-mongo. Let's let BigDecimal throw an error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for now it's better practice to return nil, and we should file a ticket to follow upon Float and Integer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created https://jira.mongodb.org/browse/MONGOID-5221 to address the demongoization issue across all types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am unresolving this discussion thread because it's referenced from https://jira.mongodb.org/browse/MONGOID-5221. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've crossed out my earlier comment here--BigDecimal correctly returns |
||
BigDecimal(object.to_s) | ||
end | ||
end | ||
end | ||
|
||
# Mongoize an object of any type to how it's stored in the db as a String. | ||
# Mongoize an object of any type to how it's stored in the db. | ||
# | ||
# @example Mongoize the object. | ||
# BigDecimal.mongoize(123) | ||
# | ||
# @param [ Object ] object The object to Mongoize | ||
# | ||
# @return [ String, nil ] A String representing the object or nil. | ||
# @return [ String | BSON::Decimal128 | nil ] A String or Decimal128 | ||
# representing the object or nil. | ||
def mongoize(object) | ||
object && object.numeric? ? object.to_s : nil | ||
unless object.nil? | ||
if object.is_a?(BSON::Decimal128) | ||
object | ||
elsif Mongoid.map_big_decimal_to_decimal128 | ||
if object.is_a?(BigDecimal) | ||
BSON::Decimal128.new(object) | ||
elsif object.numeric? | ||
p-mongo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BSON::Decimal128.new(object.to_s) | ||
else | ||
object.mongoize | ||
end | ||
p-mongo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elsif object.numeric? | ||
object.to_s | ||
end | ||
p-mongo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end | ||
end | ||
|
Uh oh!
There was an error while loading. Please reload this page.