Module: Mongoid::Persistence::Atomic::Operation
- Includes:
- Atomic::Positionable
- Defined in:
- lib/mongoid/persistence/atomic/operation.rb
Overview
This is the included module for all atomic operation objects.
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#options ⇒ Object
Returns the value of attribute options.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#collection ⇒ Collection
Get the collection to be used for persistence.
-
#initialize(document, fields, value, options = {}) ⇒ Object
Initialize the new pullAll operation.
-
#operation(modifier) ⇒ Hash
Get the atomic operation to perform.
-
#path(field = field) ⇒ String, Symbol
Get the path to the field that is getting atomically updated.
-
#prepare {|document| ... } ⇒ Object
All atomic operations use this with a block to ensure saftey options clear out after the execution.
Methods included from Atomic::Positionable
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
10 11 12 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 10 def document @document end |
#fields ⇒ Object
Returns the value of attribute fields.
10 11 12 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 10 def fields @fields end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 10 def @options end |
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 10 def value @value end |
Instance Method Details
#collection ⇒ Collection
Get the collection to be used for persistence.
20 21 22 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 20 def collection document._root.collection end |
#initialize(document, fields, value, options = {}) ⇒ Object
Initialize the new pullAll operation.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 35 def initialize(document, fields, value, = {}) @document, @value = document, value @options = @fields = Array.wrap(fields).collect do |field| document.database_field_name(field.to_s) end self.class.send(:define_method, :field) do @fields.first end if @fields.length == 1 end |
#operation(modifier) ⇒ Hash
Get the atomic operation to perform.
59 60 61 62 63 64 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 59 def operation(modifier) hash = Hash[fields.collect do |field| [path(field), cast_value] end] { modifier => hash } end |
#path(field = field) ⇒ String, Symbol
Get the path to the field that is getting atomically updated.
74 75 76 77 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 74 def path(field = field) position = document.atomic_position position.blank? ? field : "#{position}.#{field}" end |
#prepare {|document| ... } ⇒ Object
All atomic operations use this with a block to ensure saftey options clear out after the execution.
90 91 92 |
# File 'lib/mongoid/persistence/atomic/operation.rb', line 90 def prepare yield(document) end |