Skip to content

Commit 2fd8ae1

Browse files
committed
MONGOID-5222 get rid of 42bogus
1 parent 875e279 commit 2fd8ae1

File tree

7 files changed

+19
-15
lines changed

7 files changed

+19
-15
lines changed

docs/release-notes/mongoid-8.0.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ functionality:
7373
+--------------+------------------------+------------------------+-------------------+
7474
| Field Type | Situation | Previous Functionality | New Functionality |
7575
+==============+========================+========================+===================+
76-
| Integer/Float| When a non-numeric | return ``nil`` | return ``42`` |
77-
| | string starts with a | | |
78-
| | number: "42bogus" | | |
79-
+--------------+------------------------+------------------------+-------------------+
8076
| Boolean | When a non-boolean | return ``false`` | return ``nil`` or |
8177
| | string is assigned: | | raise |
8278
| | "bogus value" | | ``InvalidError`` |

lib/mongoid/extensions/big_decimal.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ def mongoize(object)
7474
BSON::Decimal128.new(object)
7575
elsif object.numeric?
7676
BSON::Decimal128.new(object.to_s)
77-
elsif object.respond_to?(:to_d)
78-
BSON::Decimal128.new(object.to_d)
7977
end
8078
else
8179
if object.is_a?(BSON::Decimal128) || object.numeric?
8280
object.to_s
83-
elsif object.respond_to?(:to_d)
81+
elsif object.numeric?
8482
object.to_d.to_s
8583
end
8684
end.tap do |res|

lib/mongoid/extensions/float.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module ClassMethods
3838
def mongoize(object)
3939
return if object.nil?
4040
return if object.is_a?(String) && object.blank?
41-
if object.respond_to?(:to_f)
41+
if object.numeric?
4242
object.to_f
4343
else
4444
raise Errors::InvalidValue.new(self, object)

lib/mongoid/extensions/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module ClassMethods
4646
def mongoize(object)
4747
return if object.nil?
4848
return if object.is_a?(String) && object.blank?
49-
if object.respond_to?(:to_i)
49+
if object.numeric?
5050
object.to_i
5151
else
5252
raise Errors::InvalidValue.new(self, object)

spec/mongoid/extensions/big_decimal_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@
268268
"1a2"
269269
end
270270

271-
it "returns a string" do
272-
expect(mongoized).to eq("1.0")
271+
it "raises an error" do
272+
expect do
273+
mongoized
274+
end.to raise_error(Mongoid::Errors::InvalidValue)
273275
end
274276
end
275277

@@ -711,7 +713,9 @@
711713
end
712714

713715
it "returns a decimal128" do
714-
expect(mongoized).to eq(BSON::Decimal128.new("1"))
716+
expect do
717+
mongoized
718+
end.to raise_error(Mongoid::Errors::InvalidValue)
715719
end
716720
end
717721

spec/mongoid/extensions/float_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
context "when the value is not a float string" do
6262

6363
it "return 0" do
64-
expect(Float.demongoize('asdf')).to eq(0)
64+
expect do
65+
Float.demongoize('asdf')
66+
end.to raise_error(Mongoid::Errors::InvalidValue)
6567
end
6668
end
6769
end
@@ -125,7 +127,9 @@
125127
context "when the string is non numerical" do
126128

127129
it "return 0" do
128-
expect(Float.mongoize("foo")).to eq(0)
130+
expect do
131+
Float.mongoize("foo")
132+
end.to raise_error(Mongoid::Errors::InvalidValue)
129133
end
130134
end
131135

spec/mongoid/extensions/integer_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@
112112
context "when the string is non numerical" do
113113

114114
it "returns 0" do
115-
expect(Integer.mongoize("foo")).to eq(0)
115+
expect do
116+
Integer.mongoize("foo")
117+
end.to raise_error(Mongoid::Errors::InvalidValue)
116118
end
117119
end
118120

0 commit comments

Comments
 (0)