Skip to content

Commit c51cd56

Browse files
committed
MONGOID-5222 remove _mongoid_wrap_mongoize
1 parent c53dc92 commit c51cd56

File tree

15 files changed

+105
-103
lines changed

15 files changed

+105
-103
lines changed

lib/mongoid/criteria/queryable/extensions/boolean.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ module ClassMethods
2020
# @return [ true, false ] The boolean value.
2121
def evolve(object)
2222
__evolve__(object) do |obj|
23-
if obj.to_s =~ (/\A(true|t|yes|y|on|1|1.0)\z/i)
24-
true
25-
elsif obj.to_s =~ (/\A(false|f|no|n|off|0|0.0)\z/i)
26-
false
23+
begin
24+
mongoize(object)
25+
rescue InvalidValue
26+
object
2727
end
2828
end
2929
end

lib/mongoid/extensions/array.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ def __mongoize_fk__(association, object)
146146
#
147147
# @return [ Array ] The object mongoized.
148148
def mongoize(object)
149-
_mongoid_wrap_mongoize(object) do
150-
if object.is_a?(::Array)
151-
evolve(object).collect{ |obj| obj.class.mongoize(obj) }
152-
end
149+
return if object.nil?
150+
if object.is_a?(::Array)
151+
evolve(object).collect{ |obj| obj.class.mongoize(obj) }
152+
else
153+
raise Errors::InvalidValue.new(self, object)
153154
end
154155
end
155156

lib/mongoid/extensions/big_decimal.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,27 @@ def demongoize(object)
6565
# @return [ String | BSON::Decimal128 | nil ] A String or Decimal128
6666
# representing the object or nil.
6767
def mongoize(object)
68-
return nil if object.is_a?(String) && object.blank?
69-
_mongoid_wrap_mongoize(object) do
70-
if Mongoid.map_big_decimal_to_decimal128
71-
if object.is_a?(BSON::Decimal128)
72-
object
73-
elsif object.is_a?(BigDecimal)
74-
BSON::Decimal128.new(object)
75-
elsif object.numeric?
76-
BSON::Decimal128.new(object.to_s)
77-
elsif object.respond_to?(:to_d)
78-
BSON::Decimal128.new(object.to_d)
79-
end
80-
else
81-
if object.is_a?(BSON::Decimal128) || object.numeric?
82-
object.to_s
83-
elsif object.respond_to?(:to_d)
84-
object.to_d.to_s
85-
end
68+
return if object.nil?
69+
return if object.is_a?(String) && object.blank?
70+
if Mongoid.map_big_decimal_to_decimal128
71+
if object.is_a?(BSON::Decimal128)
72+
object
73+
elsif object.is_a?(BigDecimal)
74+
BSON::Decimal128.new(object)
75+
elsif object.numeric?
76+
BSON::Decimal128.new(object.to_s)
77+
elsif object.respond_to?(:to_d)
78+
BSON::Decimal128.new(object.to_d)
79+
end
80+
else
81+
if object.is_a?(BSON::Decimal128) || object.numeric?
82+
object.to_s
83+
elsif object.respond_to?(:to_d)
84+
object.to_d.to_s
85+
end
86+
end.tap do |res|
87+
if res.nil?
88+
raise Errors::InvalidValue.new(self, object)
8689
end
8790
end
8891
end

lib/mongoid/extensions/binary.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ module ClassMethods
2727
# @return [ String | Symbol | BSON::Binary | nil ] A String or Binary
2828
# representing the object or nil.
2929
def mongoize(object)
30-
_mongoid_wrap_mongoize(object) do
31-
case object
32-
when BSON::Binary
33-
object
34-
when String, Symbol
35-
BSON::Binary.new(object.to_s)
36-
end
30+
return if object.nil?
31+
case object
32+
when BSON::Binary then object
33+
when String, Symbol then BSON::Binary.new(object.to_s)
34+
else raise Errors::InvalidValue.new(self, object)
3735
end
3836
end
3937
end

lib/mongoid/extensions/boolean.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ class << self
1313
#
1414
# @return [ String ] The object mongoized.
1515
def mongoize(object)
16-
_mongoid_wrap_mongoize(object) do
17-
evolve(object)
16+
return if object.nil?
17+
if object.to_s =~ (/\A(true|t|yes|y|on|1|1.0)\z/i)
18+
true
19+
elsif object.to_s =~ (/\A(false|f|no|n|off|0|0.0)\z/i)
20+
false
21+
else
22+
raise Errors::InvalidValue.new(self, object)
1823
end
1924
end
2025
end

lib/mongoid/extensions/date.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,22 @@ def demongoize(object)
5252
#
5353
# @return [ Time ] The object mongoized.
5454
def mongoize(object)
55-
_mongoid_wrap_mongoize(object) do
56-
unless object.blank?
57-
begin
58-
if object.is_a?(String)
59-
# https://jira.mongodb.org/browse/MONGOID-4460
60-
time = ::Time.parse(object)
61-
else
62-
time = object.__mongoize_time__
63-
end
64-
if time.acts_like?(:time)
65-
::Time.utc(time.year, time.month, time.day)
66-
end
67-
rescue ArgumentError
68-
nil
69-
end
55+
return if object.nil? || object.blank?
56+
begin
57+
if object.is_a?(String)
58+
# https://jira.mongodb.org/browse/MONGOID-4460
59+
time = ::Time.parse(object)
60+
else
61+
time = object.__mongoize_time__
62+
end
63+
if time.acts_like?(:time)
64+
::Time.utc(time.year, time.month, time.day)
65+
end
66+
rescue ArgumentError
67+
nil
68+
end.tap do |res|
69+
if res.nil?
70+
raise Errors::InvalidValue.new(self, object)
7071
end
7172
end
7273
end

lib/mongoid/extensions/float.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ module ClassMethods
3636
#
3737
# @return [ String ] The object mongoized.
3838
def mongoize(object)
39-
return nil if object.is_a?(String) && object.blank?
40-
_mongoid_wrap_mongoize(object) do
41-
if object.respond_to?(:to_f)
42-
object.to_f
43-
end
39+
return if object.nil?
40+
return if object.is_a?(String) && object.blank?
41+
if object.respond_to?(:to_f)
42+
object.to_f
43+
else
44+
raise Errors::InvalidValue.new(self, object)
4445
end
4546
end
4647
alias :demongoize :mongoize

lib/mongoid/extensions/hash.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ module ClassMethods
219219
#
220220
# @return [ Hash ] The object mongoized.
221221
def mongoize(object)
222-
_mongoid_wrap_mongoize(object) do
223-
if object.is_a?(Hash)
224-
evolve(object.dup).transform_values!(&:mongoize)
225-
end
222+
return if object.nil?
223+
if object.is_a?(Hash)
224+
evolve(object.dup).transform_values!(&:mongoize)
225+
else
226+
raise Errors::InvalidValue.new(self, object)
226227
end
227228
end
228229

lib/mongoid/extensions/integer.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ module ClassMethods
4444
#
4545
# @return [ String ] The object mongoized.
4646
def mongoize(object)
47-
return nil if object.is_a?(String) && object.blank?
48-
_mongoid_wrap_mongoize(object) do
49-
if object.respond_to?(:to_i)
50-
object.to_i
51-
end
47+
return if object.nil?
48+
return if object.is_a?(String) && object.blank?
49+
if object.respond_to?(:to_i)
50+
object.to_i
51+
else
52+
raise Errors::InvalidValue.new(self, object)
5253
end
5354
end
5455
alias :demongoize :mongoize

lib/mongoid/extensions/object.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,6 @@ def demongoize(object)
239239
def mongoize(object)
240240
object.mongoize
241241
end
242-
243-
def _mongoid_wrap_mongoize(object)
244-
return if object.nil?
245-
yield.tap do |res|
246-
if res.nil?
247-
raise Errors::InvalidValue.new(self, object)
248-
end
249-
end
250-
end
251242
end
252243
end
253244
end

0 commit comments

Comments
 (0)