Skip to content

Commit

Permalink
MONGOID-5662 Deprecate Object#__to_inc__ and BigDecimal#__to_inc__ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis authored Nov 7, 2023
1 parent 5dff6d6 commit f99e917
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
15 changes: 11 additions & 4 deletions lib/mongoid/extensions/big_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@

module Mongoid
module Extensions

# Adds type-casting behavior to BigDecimal class.
module BigDecimal
# Behavior to be invoked when the module is included.
#
# @param [ Module ] base the class or module doing the including
#
# @api private
def self.included(base)
base.extend(ClassMethods)
end

# Convert the big decimal to an $inc-able value.
#
# @example Convert the big decimal.
# bd.__to_inc__
#
# @return [ Float ] The big decimal as a float.
# @deprecated
def __to_inc__
to_f
end
Mongoid.deprecate(self, :__to_inc__)

# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
Expand All @@ -39,7 +48,6 @@ def numeric?
end

module ClassMethods

# Convert the object from its mongo friendly ruby type to this type.
#
# @param [ Object ] object The object to demongoize.
Expand Down Expand Up @@ -89,5 +97,4 @@ def mongoize(object)
end
end

::BigDecimal.__send__(:include, Mongoid::Extensions::BigDecimal)
::BigDecimal.extend(Mongoid::Extensions::BigDecimal::ClassMethods)
BigDecimal.include Mongoid::Extensions::BigDecimal
13 changes: 7 additions & 6 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

module Mongoid
module Extensions

# Adds type-casting behavior to Object class.
module Object
def self.included(base)
base.extend(ClassMethods)
end

# Evolve a plain object into an object id.
#
Expand Down Expand Up @@ -76,9 +78,11 @@ def __sortable__
# 1.__to_inc__
#
# @return [ Object ] The object.
# @deprecated
def __to_inc__
self
end
Mongoid.deprecate(self, :__to_inc__)

# Checks whether conditions given in this object are known to be
# unsatisfiable, i.e., querying with this object will always return no
Expand All @@ -93,6 +97,7 @@ def __to_inc__
def blank_criteria?
false
end
Mongoid.deprecate(self, :blank_criteria?)

# Do or do not, there is no try. -- Yoda.
#
Expand Down Expand Up @@ -210,7 +215,6 @@ def you_must(name, *args)
end

module ClassMethods

# Convert the provided object to a foreign key, given the metadata key
# contstraint.
#
Expand Down Expand Up @@ -255,7 +259,4 @@ def mongoize(object)
end
end

::Object.__send__(:include, Mongoid::Extensions::Object)
::Object.extend(Mongoid::Extensions::Object::ClassMethods)

::Mongoid.deprecate(Object, :blank_criteria)
Object.include Mongoid::Extensions::Object
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/incrementable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Incrementable
def inc(increments)
prepare_atomic_operation do |ops|
process_atomic_operations(increments) do |field, value|
increment = value.__to_inc__
increment = value.is_a?(BigDecimal) ? value.to_f : value
current = attributes[field]
new_value = (current || 0) + increment
process_attribute field, new_value if executing_atomically?
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/multipliable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Multipliable
def mul(factors)
prepare_atomic_operation do |ops|
process_atomic_operations(factors) do |field, value|
factor = value.__to_inc__
factor = value.is_a?(BigDecimal) ? value.to_f : value
current = attributes[field]
new_value = (current || 0) * factor
process_attribute field, new_value if executing_atomically?
Expand Down

0 comments on commit f99e917

Please sign in to comment.