Skip to content

Commit

Permalink
Merge pull request #4164 from johnnyshields/big-decimal
Browse files Browse the repository at this point in the history
MONGOID-4165 Improve BigDecimal mongoize behavior
  • Loading branch information
estolfo committed Apr 21, 2016
2 parents d043c55 + d879ee3 commit b1866c2
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 41 deletions.
25 changes: 17 additions & 8 deletions lib/mongoid/extensions/big_decimal.rb
Expand Up @@ -28,6 +28,18 @@ def mongoize
to_s
end

# Is the BigDecimal a number?
#
# @example Is the object a number?.
# object.numeric?
#
# @return [ true ] Always true.
#
# @since 6.0.0
def numeric?
true
end

module ClassMethods

# Convert the object from its mongo friendly ruby type to this type.
Expand All @@ -37,28 +49,25 @@ module ClassMethods
#
# @param [ Object ] object The object to demongoize.
#
# @return [ Object ] The object.
# @return [ BigDecimal, nil ] A BigDecimal derived from the object or nil.
#
# @since 3.0.0
def demongoize(object)
if object
object.numeric? ? ::BigDecimal.new(object.to_s) : object
end
object && object.numeric? ? ::BigDecimal.new(object.to_s) : nil
end

# Mongoize an object of any type to how it's stored in the db as a big
# decimal.
# Mongoize an object of any type to how it's stored in the db as a String.
#
# @example Mongoize the object.
# BigDecimal.mongoize(123)
#
# @param [ Object ] object The object to Mongoize
#
# @return [ String ] The mongoized object.
# @return [ String, nil ] A String representing the object or nil.
#
# @since 3.0.7
def mongoize(object)
object ? object.to_s : object
object && object.numeric? ? object.to_s : nil
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/extensions/string.rb
Expand Up @@ -72,7 +72,8 @@ def mongoid_id?
self =~ /\A(|_)id$/
end

# Is the string a number?
# Is the string a number? The literals "NaN", "Infinity", and "-Infinity"
# are counted as numbers.
#
# @example Is the string a number.
# "1234.23".numeric?
Expand All @@ -81,7 +82,7 @@ def mongoid_id?
#
# @since 3.0.0
def numeric?
true if Float(self) rescue (self == "NaN")
true if Float(self) rescue (self =~ /^NaN|\-?Infinity$/)
end

# Get the string as a getter string.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/findable.rb
Expand Up @@ -11,7 +11,7 @@ module Findable
select_with :with_default_scope

# These are methods defined on the criteria that should also be accessible
# directly from the the class level.
# directly from the class level.
delegate \
:aggregates,
:avg,
Expand Down
18 changes: 9 additions & 9 deletions spec/mongoid/attributes/nested_spec.rb
Expand Up @@ -1970,7 +1970,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.addresses.size).to eq(1)
end

Expand Down Expand Up @@ -2031,7 +2031,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.addresses.first.street).to eq("Maybachufer")
end

Expand Down Expand Up @@ -2091,7 +2091,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.addresses.first.street).to eq("Maybachufer")
end

Expand Down Expand Up @@ -3431,7 +3431,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.posts.size).to eq(1)
end

Expand Down Expand Up @@ -3492,7 +3492,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.posts.first.title).to eq("New Blog")
end

Expand Down Expand Up @@ -3552,7 +3552,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.posts.first.title).to eq("New Blog")
end

Expand Down Expand Up @@ -4097,7 +4097,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.preferences.size).to eq(1)
end

Expand Down Expand Up @@ -4158,7 +4158,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.preferences.first.name).to eq("New Blog")
end

Expand Down Expand Up @@ -4218,7 +4218,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.preferences.first.name).to eq("New Blog")
end

Expand Down

0 comments on commit b1866c2

Please sign in to comment.