Skip to content

Commit

Permalink
Use Array#wrap with $each method to be safe. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Jan 7, 2018
1 parent 26c031b commit 98bdf06
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

#### 0.3.1

* Use Array#wrap with `$each` method to be safe.

#### 0.3.0

* Replace `$pushAll` with `$push + $each`.
Expand Down
8 changes: 4 additions & 4 deletions lib/patches/atomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def push_all(*args)
if args.length == 1 && args.first.is_a?(Hash)
query.update_all("$push" => collect_operations(args.first, true))
else
query.update_all("$push" => { database_field_name(args[0]) => { "$each" => args[1] } })
query.update_all("$push" => { database_field_name(args[0]) => { "$each" => Array.wrap(args[1]) } })
end
end

Expand Down Expand Up @@ -104,7 +104,7 @@ def set_with_mongoid4(*args)

def collect_operations(ops, use_each = false)
ops.inject({}) do |operations, (field, value)|
operations[database_field_name(field)] = use_each ? { '$each' => value.mongoize } : value.mongoize
operations[database_field_name(field)] = use_each ? { '$each' => Array.wrap(value.mongoize) } : value.mongoize
operations
end
end
Expand All @@ -123,7 +123,7 @@ def persist
end

def operation(modifier)
{ modifier => { path => value.is_a?(Array) ? { "$each" => value } : value}}
{ modifier => { path => { "$each" => Array.wrap(value) } } }
end
end

Expand Down Expand Up @@ -359,7 +359,7 @@ def execute_batch_insert(docs, operation, use_each = false)
inserts = pre_process_batch_insert(docs)
if insertable?
collection.find(selector).update(
positionally(selector, operation => { path => use_each ? { '$each' => inserts } : inserts })
positionally(selector, operation => { path => use_each ? { '$each' => Array.wrap(inserts) } : inserts })
)
post_process_batch_insert(docs)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MongoidMonkey
VERSION = '0.3.0'
VERSION = '0.3.1'
end

0 comments on commit 98bdf06

Please sign in to comment.