Class: MmPartialUpdate::UpdateCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/mm_partial_update/update_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commandsObject (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_documentObject (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_documentObject (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_selectorObject



11
12
13
# File 'lib/mm_partial_update/update_command.rb', line 11

def document_selector
  {:_id=>root_document._id}
end

#empty?Boolean

Returns:

  • (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(options={})
   #if there are no commands, there is nothing to do...
  return if empty?

  selector = document_selector.tap {|s|s.merge!("$atomic"=>true) if options[:atomic]}

  dbcommands = prepare_mongodb_commands

  dbcommands.each do |command|
    root_document.collection.update(selector, command, :multi=>false,
                                    :upsert=>true, :safe=>options[: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

#resetObject



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, options={})

  return if fields.blank?

  selector = selector.to_s if selector

  if selector.blank? && options[:replace]
    commands["$set"] = fields
  elsif selector.blank?
    commands["$set"].merge!(fields)
  elsif options[: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_hObject



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, options={})
  raise "'unset' requires a non-blank selector" if selector.blank?
  selector = selector.to_s
  options[:nullify] ? commands["$set"][selector] = nil : commands["$unset"][selector] = true
end