File tree Expand file tree Collapse file tree 7 files changed +19
-15
lines changed Expand file tree Collapse file tree 7 files changed +19
-15
lines changed Original file line number Diff line number Diff line change @@ -73,10 +73,6 @@ functionality:
73
73
+--------------+------------------------+------------------------+-------------------+
74
74
| Field Type | Situation | Previous Functionality | New Functionality |
75
75
+==============+========================+========================+===================+
76
- | Integer/Float| When a non-numeric | return ``nil`` | return ``42`` |
77
- | | string starts with a | | |
78
- | | number: "42bogus" | | |
79
- +--------------+------------------------+------------------------+-------------------+
80
76
| Boolean | When a non-boolean | return ``false`` | return ``nil`` or |
81
77
| | string is assigned: | | raise |
82
78
| | "bogus value" | | ``InvalidError`` |
Original file line number Diff line number Diff line change @@ -74,13 +74,11 @@ def mongoize(object)
74
74
BSON ::Decimal128 . new ( object )
75
75
elsif object . numeric?
76
76
BSON ::Decimal128 . new ( object . to_s )
77
- elsif object . respond_to? ( :to_d )
78
- BSON ::Decimal128 . new ( object . to_d )
79
77
end
80
78
else
81
79
if object . is_a? ( BSON ::Decimal128 ) || object . numeric?
82
80
object . to_s
83
- elsif object . respond_to? ( :to_d )
81
+ elsif object . numeric?
84
82
object . to_d . to_s
85
83
end
86
84
end . tap do |res |
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ module ClassMethods
38
38
def mongoize ( object )
39
39
return if object . nil?
40
40
return if object . is_a? ( String ) && object . blank?
41
- if object . respond_to? ( :to_f )
41
+ if object . numeric?
42
42
object . to_f
43
43
else
44
44
raise Errors ::InvalidValue . new ( self , object )
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ module ClassMethods
46
46
def mongoize ( object )
47
47
return if object . nil?
48
48
return if object . is_a? ( String ) && object . blank?
49
- if object . respond_to? ( :to_i )
49
+ if object . numeric?
50
50
object . to_i
51
51
else
52
52
raise Errors ::InvalidValue . new ( self , object )
Original file line number Diff line number Diff line change 268
268
"1a2"
269
269
end
270
270
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 )
273
275
end
274
276
end
275
277
711
713
end
712
714
713
715
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 )
715
719
end
716
720
end
717
721
Original file line number Diff line number Diff line change 61
61
context "when the value is not a float string" do
62
62
63
63
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 )
65
67
end
66
68
end
67
69
end
125
127
context "when the string is non numerical" do
126
128
127
129
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 )
129
133
end
130
134
end
131
135
Original file line number Diff line number Diff line change 112
112
context "when the string is non numerical" do
113
113
114
114
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 )
116
118
end
117
119
end
118
120
You can’t perform that action at this time.
0 commit comments