File tree Expand file tree Collapse file tree 3 files changed +20
-17
lines changed Expand file tree Collapse file tree 3 files changed +20
-17
lines changed Original file line number Diff line number Diff line change @@ -52,19 +52,21 @@ def demongoize(object)
52
52
#
53
53
# @return [ Time ] The object mongoized.
54
54
def mongoize ( object )
55
- return if object . nil? || object . blank?
56
- begin
57
- if object . is_a? ( String )
58
- # https://jira.mongodb.org/browse/MONGOID-4460
59
- time = ::Time . parse ( object )
60
- else
61
- time = object . __mongoize_time__
55
+ return if object . nil?
56
+ unless object . blank?
57
+ begin
58
+ if object . is_a? ( String )
59
+ # https://jira.mongodb.org/browse/MONGOID-4460
60
+ time = ::Time . parse ( object )
61
+ else
62
+ time = object . __mongoize_time__
63
+ end
64
+ if time . acts_like? ( :time )
65
+ ::Time . utc ( time . year , time . month , time . day )
66
+ end
67
+ rescue ArgumentError
68
+ nil
62
69
end
63
- if time . acts_like? ( :time )
64
- ::Time . utc ( time . year , time . month , time . day )
65
- end
66
- rescue ArgumentError
67
- nil
68
70
end . tap do |res |
69
71
if res . nil?
70
72
raise Errors ::InvalidValue . new ( self , object )
Original file line number Diff line number Diff line change @@ -67,7 +67,8 @@ def mongoize(object)
67
67
case object
68
68
when Hash then __mongoize_hash__ ( object )
69
69
when Range then __mongoize_range__ ( object )
70
- else raise Errors ::InvalidValue . new ( self , object )
70
+ end . tap do |res |
71
+ raise Errors ::InvalidValue . new ( self , object ) unless res
71
72
end
72
73
end
73
74
Original file line number Diff line number Diff line change 1586
1586
it "raises an error when trying to set a value of invalid type - array" do
1587
1587
expect do
1588
1588
person . map = [ ]
1589
- end . to raise_error ( Mongoid ::Errors ::InvalidValue , /Value of type Array cannot be written to a field of type Hash/ )
1589
+ end . to raise_error ( Mongoid ::Errors ::InvalidValue , /The value \[ \] cannot be written to a field of type Hash/ )
1590
1590
end
1591
1591
1592
1592
it "raises an error when trying to set a value of invalid type - boolean" do
1593
1593
expect do
1594
1594
person . map = false
1595
- end . to raise_error ( Mongoid ::Errors ::InvalidValue , /Value of type FalseClass cannot be written to a field of type Hash/ )
1595
+ end . to raise_error ( Mongoid ::Errors ::InvalidValue , /The value false cannot be written to a field of type Hash/ )
1596
1596
end
1597
1597
1598
1598
it "can set a Hash value" do
1610
1610
it "raises an error when trying to set a value of invalid type - hash" do
1611
1611
expect do
1612
1612
person . aliases = { }
1613
- end . to raise_error ( Mongoid ::Errors ::InvalidValue , /Value of type Hash cannot be written to a field of type Array/ )
1613
+ end . to raise_error ( Mongoid ::Errors ::InvalidValue , /The value {} cannot be written to a field of type Array/ )
1614
1614
end
1615
1615
1616
1616
it "raises an error when trying to set a value of invalid type - boolean" do
1617
1617
expect do
1618
1618
person . aliases = false
1619
- end . to raise_error ( Mongoid ::Errors ::InvalidValue , /Value of type FalseClass cannot be written to a field of type Array/ )
1619
+ end . to raise_error ( Mongoid ::Errors ::InvalidValue , /The value false cannot be written to a field of type Array/ )
1620
1620
end
1621
1621
end
1622
1622
You can’t perform that action at this time.
0 commit comments