File tree Expand file tree Collapse file tree 6 files changed +51
-10
lines changed Expand file tree Collapse file tree 6 files changed +51
-10
lines changed Original file line number Diff line number Diff line change @@ -72,10 +72,14 @@ def mongoize(object)
72
72
BSON ::Decimal128 . new ( object )
73
73
elsif object . numeric?
74
74
BSON ::Decimal128 . new ( object . to_s )
75
+ elsif !object . is_a? ( String )
76
+ object . try ( :to_d )
75
77
end
76
78
else
77
79
if object . is_a? ( BSON ::Decimal128 ) || object . numeric?
78
80
object . to_s
81
+ elsif !object . is_a? ( String )
82
+ object . try ( :to_d )
79
83
end
80
84
end
81
85
end
Original file line number Diff line number Diff line change @@ -37,8 +37,12 @@ module ClassMethods
37
37
# @return [ Float | nil ] The object mongoized or nil.
38
38
def mongoize ( object )
39
39
return if object . blank?
40
- if object . numeric?
41
- object . to_f
40
+ if object . is_a? ( String )
41
+ if object . numeric?
42
+ object . to_f
43
+ end
44
+ else
45
+ object . try ( :to_f )
42
46
end
43
47
end
44
48
alias :demongoize :mongoize
Original file line number Diff line number Diff line change @@ -45,8 +45,12 @@ module ClassMethods
45
45
# @return [ Integer | nil ] The object mongoized or nil.
46
46
def mongoize ( object )
47
47
return if object . blank?
48
- if object . numeric?
49
- object . to_i
48
+ if object . is_a? ( String )
49
+ if object . numeric?
50
+ object . to_i
51
+ end
52
+ else
53
+ object . try ( :to_i )
50
54
end
51
55
end
52
56
alias :demongoize :mongoize
Original file line number Diff line number Diff line change 273
273
end
274
274
end
275
275
276
+ context "when the value is castable" do
277
+
278
+ let ( :value ) do
279
+ 2 . hours
280
+ end
281
+
282
+ before do
283
+ expect ( value ) . to be_a ( ActiveSupport ::Duration )
284
+ end
285
+
286
+ it "returns nil" do
287
+ expect ( mongoized ) . to eq ( 7200 )
288
+ end
289
+ end
290
+
276
291
context "when the value is nil" do
277
292
278
293
let ( :value ) do
Original file line number Diff line number Diff line change 100
100
end
101
101
end
102
102
103
- context "when the string is numerical " do
103
+ context "when the string starts with a number " do
104
104
105
- it "returns the float value for the string " do
106
- expect ( Float . send ( method , "3 " ) ) . to eq ( 3 )
105
+ it "returns nil " do
106
+ expect ( Float . send ( method , "42bogus " ) ) . to be_nil
107
107
end
108
108
end
109
109
120
120
expect ( Float . send ( method , nil ) ) . to be_nil
121
121
end
122
122
end
123
+
124
+ context "when giving an object that is castable to an Float" do
125
+
126
+ it "returns the integer value" do
127
+ expect ( Float . send ( method , 2 . hours ) ) . to eq ( 7200 )
128
+ end
129
+ end
123
130
end
124
131
end
125
132
end
Original file line number Diff line number Diff line change 94
94
end
95
95
end
96
96
97
- context "when the string is numerical " do
97
+ context "when the string starts with a number " do
98
98
99
- it "returns the integer value for the string " do
100
- expect ( Integer . send ( method , "3 " ) ) . to eq ( 3 )
99
+ it "returns nil " do
100
+ expect ( Integer . send ( method , "42bogus " ) ) . to be_nil
101
101
end
102
102
end
103
103
114
114
expect ( Integer . send ( method , nil ) ) . to be_nil
115
115
end
116
116
end
117
+
118
+ context "when giving an object that is castable to an Integer" do
119
+
120
+ it "returns the integer value" do
121
+ expect ( Integer . send ( method , 2 . hours ) ) . to eq ( 7200 )
122
+ end
123
+ end
117
124
end
118
125
end
119
126
end
You can’t perform that action at this time.
0 commit comments