Skip to content

Commit f5bdade

Browse files
authored
MONGOID-5456 cast castable values for integer/float/big decimal (#5431)
* MONGOID-5456 cast castable values for integer/float/big decimal * MONGOID-5456 assert duration
1 parent 50147d4 commit f5bdade

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

lib/mongoid/extensions/big_decimal.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ def mongoize(object)
7272
BSON::Decimal128.new(object)
7373
elsif object.numeric?
7474
BSON::Decimal128.new(object.to_s)
75+
elsif !object.is_a?(String)
76+
object.try(:to_d)
7577
end
7678
else
7779
if object.is_a?(BSON::Decimal128) || object.numeric?
7880
object.to_s
81+
elsif !object.is_a?(String)
82+
object.try(:to_d)
7983
end
8084
end
8185
end

lib/mongoid/extensions/float.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ module ClassMethods
3737
# @return [ Float | nil ] The object mongoized or nil.
3838
def mongoize(object)
3939
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)
4246
end
4347
end
4448
alias :demongoize :mongoize

lib/mongoid/extensions/integer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ module ClassMethods
4545
# @return [ Integer | nil ] The object mongoized or nil.
4646
def mongoize(object)
4747
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)
5054
end
5155
end
5256
alias :demongoize :mongoize

spec/mongoid/extensions/big_decimal_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@
273273
end
274274
end
275275

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+
276291
context "when the value is nil" do
277292

278293
let(:value) do

spec/mongoid/extensions/float_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@
100100
end
101101
end
102102

103-
context "when the string is numerical" do
103+
context "when the string starts with a number" do
104104

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
107107
end
108108
end
109109

@@ -120,6 +120,13 @@
120120
expect(Float.send(method, nil)).to be_nil
121121
end
122122
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
123130
end
124131
end
125132
end

spec/mongoid/extensions/integer_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@
9494
end
9595
end
9696

97-
context "when the string is numerical" do
97+
context "when the string starts with a number" do
9898

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
101101
end
102102
end
103103

@@ -114,6 +114,13 @@
114114
expect(Integer.send(method, nil)).to be_nil
115115
end
116116
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
117124
end
118125
end
119126
end

0 commit comments

Comments
 (0)