File tree Expand file tree Collapse file tree 4 files changed +44
-26
lines changed Expand file tree Collapse file tree 4 files changed +44
-26
lines changed Original file line number Diff line number Diff line change @@ -65,17 +65,22 @@ def demongoize(object)
65
65
# @return [ String | BSON::Decimal128 | nil ] A String or Decimal128
66
66
# representing the object or nil.
67
67
def mongoize ( object )
68
+ return nil if object . is_a? ( String ) && object . blank?
68
69
_mongoid_wrap_mongoize ( object ) do
69
- if object . is_a? ( BSON :: Decimal128 )
70
- object
71
- elsif Mongoid . map_big_decimal_to_decimal128
72
- if object . is_a? ( BigDecimal )
70
+ if Mongoid . map_big_decimal_to_decimal128
71
+ if object . is_a? ( BSON :: Decimal128 )
72
+ object
73
+ elsif object . is_a? ( BigDecimal )
73
74
BSON ::Decimal128 . new ( object )
74
- elsif object . numeric?
75
- BSON ::Decimal128 . new ( object . to_s )
75
+ elsif object . respond_to? ( :to_d )
76
+ BSON ::Decimal128 . new ( object . to_d )
77
+ end
78
+ else
79
+ if object . is_a? ( BSON ::Decimal128 ) || object . numeric?
80
+ object . to_s
81
+ elsif object . respond_to? ( :to_d )
82
+ object . to_d . to_s
76
83
end
77
- elsif object . numeric?
78
- object . to_s
79
84
end
80
85
end
81
86
end
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ module ClassMethods
36
36
#
37
37
# @return [ String ] The object mongoized.
38
38
def mongoize ( object )
39
- return nil if object . to_s . blank?
39
+ return nil if object . is_a? ( String ) && object . blank?
40
40
_mongoid_wrap_mongoize ( object ) do
41
41
if object . respond_to? ( :to_f )
42
42
object . to_f
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ module ClassMethods
44
44
#
45
45
# @return [ String ] The object mongoized.
46
46
def mongoize ( object )
47
- return nil if object . to_s . blank?
47
+ return nil if object . is_a? ( String ) && object . blank?
48
48
_mongoid_wrap_mongoize ( object ) do
49
49
if object . respond_to? ( :to_i )
50
50
object . to_i
Original file line number Diff line number Diff line change 195
195
let ( :mongoized ) do
196
196
BigDecimal . mongoize ( value )
197
197
end
198
+
198
199
context "when the value is a BigDecimal" do
199
200
200
201
let ( :value ) do
234
235
""
235
236
end
236
237
237
- it "raises an error" do
238
- expect do
239
- mongoized
240
- end . to raise_error ( Mongoid ::Errors ::InvalidValue )
238
+ it "returns nil" do
239
+ expect ( mongoized ) . to be_nil
241
240
end
242
241
end
243
242
269
268
"1a2"
270
269
end
271
270
272
- it "raises an error" do
273
- expect do
274
- mongoized
275
- end . to raise_error ( Mongoid ::Errors ::InvalidValue )
271
+ it "returns a string" do
272
+ expect ( mongoized ) . to eq ( "1.0" )
276
273
end
277
274
end
278
275
432
429
expect ( mongoized ) . to eq ( "-Infinity" )
433
430
end
434
431
end
432
+
433
+ context "when the value is a decimal128" do
434
+ let ( :value ) do
435
+ BSON ::Decimal128 . new ( "42" )
436
+ end
437
+
438
+ it "returns a String" do
439
+ expect ( mongoized ) . to eq ( "42" )
440
+ end
441
+ end
435
442
end
436
443
437
444
describe "#mongoize" do
670
677
""
671
678
end
672
679
673
- it "raises an error" do
674
- expect do
675
- mongoized
676
- end . to raise_error ( Mongoid ::Errors ::InvalidValue )
680
+ it "returns nil" do
681
+ expect ( mongoized ) . to be_nil
677
682
end
678
683
end
679
684
705
710
"1a2"
706
711
end
707
712
708
- it "raises an error" do
709
- expect do
710
- mongoized
711
- end . to raise_error ( Mongoid ::Errors ::InvalidValue )
713
+ it "returns a decimal128" do
714
+ expect ( mongoized ) . to eq ( BSON ::Decimal128 . new ( "1" ) )
712
715
end
713
716
end
714
717
869
872
expect ( mongoized ) . to eq ( BSON ::Decimal128 . new ( "-Infinity" ) )
870
873
end
871
874
end
875
+
876
+ context "when the value is a decimal128" do
877
+ let ( :value ) do
878
+ BSON ::Decimal128 . new ( "42" )
879
+ end
880
+
881
+ it "returns a String" do
882
+ expect ( mongoized ) . to eq ( value )
883
+ end
884
+ end
872
885
end
873
886
874
887
describe "#mongoize" do
You can’t perform that action at this time.
0 commit comments