Skip to content

Commit f99e917

Browse files
authored
MONGOID-5662 Deprecate Object#__to_inc__ and BigDecimal#__to_inc__ (#5741)
1 parent 5dff6d6 commit f99e917

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

lib/mongoid/extensions/big_decimal.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@
33

44
module Mongoid
55
module Extensions
6-
76
# Adds type-casting behavior to BigDecimal class.
87
module BigDecimal
8+
# Behavior to be invoked when the module is included.
9+
#
10+
# @param [ Module ] base the class or module doing the including
11+
#
12+
# @api private
13+
def self.included(base)
14+
base.extend(ClassMethods)
15+
end
916

1017
# Convert the big decimal to an $inc-able value.
1118
#
1219
# @example Convert the big decimal.
1320
# bd.__to_inc__
1421
#
1522
# @return [ Float ] The big decimal as a float.
23+
# @deprecated
1624
def __to_inc__
1725
to_f
1826
end
27+
Mongoid.deprecate(self, :__to_inc__)
1928

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

4150
module ClassMethods
42-
4351
# Convert the object from its mongo friendly ruby type to this type.
4452
#
4553
# @param [ Object ] object The object to demongoize.
@@ -89,5 +97,4 @@ def mongoize(object)
8997
end
9098
end
9199

92-
::BigDecimal.__send__(:include, Mongoid::Extensions::BigDecimal)
93-
::BigDecimal.extend(Mongoid::Extensions::BigDecimal::ClassMethods)
100+
BigDecimal.include Mongoid::Extensions::BigDecimal

lib/mongoid/extensions/object.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
module Mongoid
55
module Extensions
6-
76
# Adds type-casting behavior to Object class.
87
module Object
8+
def self.included(base)
9+
base.extend(ClassMethods)
10+
end
911

1012
# Evolve a plain object into an object id.
1113
#
@@ -76,9 +78,11 @@ def __sortable__
7678
# 1.__to_inc__
7779
#
7880
# @return [ Object ] The object.
81+
# @deprecated
7982
def __to_inc__
8083
self
8184
end
85+
Mongoid.deprecate(self, :__to_inc__)
8286

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

97102
# Do or do not, there is no try. -- Yoda.
98103
#
@@ -210,7 +215,6 @@ def you_must(name, *args)
210215
end
211216

212217
module ClassMethods
213-
214218
# Convert the provided object to a foreign key, given the metadata key
215219
# contstraint.
216220
#
@@ -255,7 +259,4 @@ def mongoize(object)
255259
end
256260
end
257261

258-
::Object.__send__(:include, Mongoid::Extensions::Object)
259-
::Object.extend(Mongoid::Extensions::Object::ClassMethods)
260-
261-
::Mongoid.deprecate(Object, :blank_criteria)
262+
Object.include Mongoid::Extensions::Object

lib/mongoid/persistable/incrementable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Incrementable
2121
def inc(increments)
2222
prepare_atomic_operation do |ops|
2323
process_atomic_operations(increments) do |field, value|
24-
increment = value.__to_inc__
24+
increment = value.is_a?(BigDecimal) ? value.to_f : value
2525
current = attributes[field]
2626
new_value = (current || 0) + increment
2727
process_attribute field, new_value if executing_atomically?

lib/mongoid/persistable/multipliable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Multipliable
2121
def mul(factors)
2222
prepare_atomic_operation do |ops|
2323
process_atomic_operations(factors) do |field, value|
24-
factor = value.__to_inc__
24+
factor = value.is_a?(BigDecimal) ? value.to_f : value
2525
current = attributes[field]
2626
new_value = (current || 0) * factor
2727
process_attribute field, new_value if executing_atomically?

0 commit comments

Comments
 (0)