Skip to content
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

MONGOID-5662 Deprecate Object#__to_inc__ and BigDecimal#__to_inc__ #5741

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -70,9 +72,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 @@ -87,6 +91,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 @@ -202,7 +207,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 @@ -247,7 +251,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