Class: Mongoid::Atomic::Modifiers

Inherits:
Hash
  • Object
show all
Defined in:
build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb

Overview

This class contains the logic for supporting atomic operations against the database.

Instance Method Summary collapse

Instance Method Details

#add_to_set(modifications) ⇒ Object

Add the atomic $addToSet modifiers to the hash.

Examples:

Add the $addToSet modifiers.

modifiers.add_to_set({ "preference_ids" => [ "one" ] })

Parameters:

  • modifications (Hash)

    The add to set modifiers.

Since:

  • 2.4.0



19
20
21
22
23
24
25
26
27
28
29
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 19

def add_to_set(modifications)
  modifications.each_pair do |field, value|
    if add_to_sets.has_key?(field)
      value.each do |val|
        add_to_sets[field]["$each"].push(val)
      end
    else
      add_to_sets[field] = { "$each" => value }
    end
  end
end

#pull(modifications) ⇒ Object

Adds pull all modifiers to the modifiers hash.

Examples:

Add pull all operations.

modifiers.pull({ "addresses" => { "_id" => { "$in" => [ 1, 2, 3 ]}}})

Parameters:

  • modifications (Hash)

    The pull all modifiers.

Since:

  • 3.0.0



54
55
56
57
58
59
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 54

def pull(modifications)
  modifications.each_pair do |field, value|
    pulls[field] = value
    pull_fields[field.split(".", 2)[0]] = field
  end
end

#pull_all(modifications) ⇒ Object

Adds pull all modifiers to the modifiers hash.

Examples:

Add pull all operations.

modifiers.pull_all({ "addresses" => { "street" => "Bond" }})

Parameters:

  • modifications (Hash)

    The pull all modifiers.

Since:

  • 3.0.0



39
40
41
42
43
44
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 39

def pull_all(modifications)
  modifications.each_pair do |field, value|
    add_operation(pull_alls, field, value)
    pull_fields[field.split(".", 2)[0]] = field
  end
end

#push(modifications) ⇒ Object

Adds push modifiers to the modifiers hash.

Examples:

Add push operations.

modifiers.push({ "addresses" => { "street" => "Bond" }})

Parameters:

  • modifications (Hash)

    The push modifiers.

Since:

  • 2.1.0



69
70
71
72
73
74
75
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 69

def push(modifications)
  modifications.each_pair do |field, value|
    push_fields[field] = field
    mods = push_conflict?(field) ? conflicting_pushes : pushes
    add_operation(mods, field, { '$each' => Array.wrap(value) })
  end
end

#set(modifications) ⇒ Object

Adds set operations to the modifiers hash.

Examples:

Add set operations.

modifiers.set({ "title" => "sir" })

Parameters:

  • modifications (Hash)

    The set modifiers.

Since:

  • 2.1.0



85
86
87
88
89
90
91
92
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 85

def set(modifications)
  modifications.each_pair do |field, value|
    next if field == "_id"
    mods = set_conflict?(field) ? conflicting_sets : sets
    add_operation(mods, field, value)
    set_fields[field.split(".", 2)[0]] = field
  end
end

#unset(modifications) ⇒ Object

Adds unset operations to the modifiers hash.

Examples:

Add unset operations.

modifiers.unset([ "addresses" ])

Parameters:

  • modifications (Array<String>)

    The unset association names.

Since:

  • 2.2.0



102
103
104
105
106
# File 'build/mongoid-7.3/lib/mongoid/atomic/modifiers.rb', line 102

def unset(modifications)
  modifications.each do |field|
    unsets.update(field => true)
  end
end