Class: MmPartialUpdate::UpdateCommand
- Defined in:
- lib/mm_partial_update/update_command.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#root_document ⇒ Object
readonly
Returns the value of attribute root_document.
-
#target_document ⇒ Object
readonly
Returns the value of attribute target_document.
Instance Method Summary collapse
- #document_selector ⇒ Object
- #empty? ⇒ Boolean
- #execute(options = {}) ⇒ Object
-
#initialize(target_document) ⇒ UpdateCommand
constructor
A new instance of UpdateCommand.
- #merge(other_command) ⇒ Object
- #pull(selector, document_id) ⇒ Object
- #push(selector, document) ⇒ Object
- #reset ⇒ Object
- #set(selector, fields, options = {}) ⇒ Object
- #to_h ⇒ Object
- #unset(selector, options = {}) ⇒ Object
Constructor Details
#initialize(target_document) ⇒ UpdateCommand
Returns a new instance of UpdateCommand.
5 6 7 8 9 |
# File 'lib/mm_partial_update/update_command.rb', line 5 def initialize(target_document) @target_document = target_document @root_document = target_document.send(:_root_document) @commands = BSON::OrderedHash.new { |hash,key| hash[key] = BSON::OrderedHash.new } end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
3 4 5 |
# File 'lib/mm_partial_update/update_command.rb', line 3 def commands @commands end |
#root_document ⇒ Object (readonly)
Returns the value of attribute root_document.
3 4 5 |
# File 'lib/mm_partial_update/update_command.rb', line 3 def root_document @root_document end |
#target_document ⇒ Object (readonly)
Returns the value of attribute target_document.
3 4 5 |
# File 'lib/mm_partial_update/update_command.rb', line 3 def target_document @target_document end |
Instance Method Details
#document_selector ⇒ Object
11 12 13 |
# File 'lib/mm_partial_update/update_command.rb', line 11 def document_selector {:_id=>root_document._id} end |
#empty? ⇒ Boolean
61 62 63 |
# File 'lib/mm_partial_update/update_command.rb', line 61 def empty? commands.blank? end |
#execute(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mm_partial_update/update_command.rb', line 69 def execute(={}) #if there are no commands, there is nothing to do... return if empty? selector = document_selector.tap {|s|s.merge!("$atomic"=>true) if [:atomic]} dbcommands = prepare_mongodb_commands dbcommands.each do |command| root_document.collection.update(selector, command, :multi=>false, :upsert=>true, :safe=>[:safe]) end end |
#merge(other_command) ⇒ Object
53 54 55 |
# File 'lib/mm_partial_update/update_command.rb', line 53 def merge(other_command) commands.merge! other_command.to_h end |
#pull(selector, document_id) ⇒ Object
47 48 49 50 51 |
# File 'lib/mm_partial_update/update_command.rb', line 47 def pull(selector, document_id) raise "'pull' requires a non-blank selector" if selector.blank? selector = selector.to_s (commands[:pulls][selector] ||= []) << document_id end |
#push(selector, document) ⇒ Object
41 42 43 44 45 |
# File 'lib/mm_partial_update/update_command.rb', line 41 def push(selector, document) raise "'push' requires a non-blank selector" if selector.blank? selector = selector.to_s (commands[:pushes][selector] ||= []) << document end |
#reset ⇒ Object
65 66 67 |
# File 'lib/mm_partial_update/update_command.rb', line 65 def reset commands.clear end |
#set(selector, fields, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mm_partial_update/update_command.rb', line 15 def set(selector, fields, ={}) return if fields.blank? selector = selector.to_s if selector if selector.blank? && [:replace] commands["$set"] = fields elsif selector.blank? commands["$set"].merge!(fields) elsif [:replace] commands["$set"][selector] = fields else commands["$set"].merge!(fields.keys.inject(BSON::OrderedHash.new) do |hash,field_name| hash["#{selector}.#{field_name}"] = fields[field_name] hash end) end end |
#to_h ⇒ Object
57 58 59 |
# File 'lib/mm_partial_update/update_command.rb', line 57 def to_h commands end |
#unset(selector, options = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/mm_partial_update/update_command.rb', line 35 def unset(selector, ={}) raise "'unset' requires a non-blank selector" if selector.blank? selector = selector.to_s [:nullify] ? commands["$set"][selector] = nil : commands["$unset"][selector] = true end |