123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Atomic::Modifiers

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Hash
  • Object
Defined in: lib/mongoid/atomic/modifiers.rb

Overview

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

Class Attribute Summary

::Mongoid::Extensions::Hash::ClassMethods - Extended

resizable?

Can the size of this object change?

Class Method Summary

::Mongoid::Extensions::Hash::ClassMethods - Extended

mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

Instance Attribute Summary

::Mongoid::Extensions::Hash - Included

#resizable?

Can the size of this object change?

Instance Method Summary

::Mongoid::Extensions::Hash - Included

#__consolidate__

Consolidate the key/values in the hash under an atomic $set.

#__evolve_object_id__

Evolves each value in the hash to an object id if it is convertable.

#__mongoize_object_id__

Mongoizes each value in the hash to an object id if it is convertable.

#delete_id

Deletes an id value from the hash.

#extract_id

Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol.

#mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

#to_criteria

Convert this hash to a criteria.

Instance Method Details

#add_each_operation(mods, field, value) (private)

Adds or appends an array operation with the $each specifier used in conjuction with $push.

Examples:

Add the operation.

modifications.add_operation(mods, field, value)

Parameters:

  • mods (Hash)

    The modifications.

  • field (String)

    The field.

  • value (Hash)

    The atomic op.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 130

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

#add_operation(mods, field, value) (private)

Add the operation to the modifications, either appending or creating a new one.

Examples:

Add the operation.

modifications.add_operation(mods, field, value)

Parameters:

  • mods (Hash)

    The modifications.

  • field (String)

    The field.

  • value (Hash)

    The atomic op.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 107

def add_operation(mods, field, value)
  if mods.has_key?(field)
    if mods[field].is_a?(Array)
      value.each do |val|
        mods[field].push(val)
      end
    elsif mods[field]['$each']
      mods[field]['$each'].concat(value['$each'])
    end
  else
    mods[field] = value
  end
end

#add_to_set(modifications)

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.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 17

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

#add_to_setsHash (private)

Get the $addToSet operations or initialize a new one.

Examples:

Get the $addToSet operations.

modifiers.add_to_sets

Returns:

  • (Hash)

    The $addToSet operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 146

def add_to_sets
  self["$addToSet"] ||= {}
end

#conflicting_pullsHash (private)

Get the conflicting pull modifications.

Examples:

Get the conflicting pulls.

modifiers.conflicting_pulls

Returns:

  • (Hash)

    The conflicting pull operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 183

def conflicting_pulls
  conflicts["$pullAll"] ||= {}
end

#conflicting_pushesHash (private)

Get the conflicting push modifications.

Examples:

Get the conflicting pushs.

modifiers.conflicting_pushs

Returns:

  • (Hash)

    The conflicting push operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 193

def conflicting_pushes
  conflicts["$push"] ||= {}
end

#conflicting_setsHash (private)

Get the conflicting set modifications.

Examples:

Get the conflicting sets.

modifiers.conflicting_sets

Returns:

  • (Hash)

    The conflicting set operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 203

def conflicting_sets
  conflicts["$set"] ||= {}
end

#conflictsHash (private)

Get the push operations that would have conflicted with the sets.

Examples:

Get the conflicts.

modifiers.conflicts

Returns:

  • (Hash)

    The conflicting modifications.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 213

def conflicts
  self[:conflicts] ||= {}
end

#pull(modifications)

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.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 48

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

#pull_all(modifications)

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.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 35

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

#pull_allsHash (private)

Get the $pullAll operations or initialize a new one.

Examples:

Get the $pullAll operations.

modifiers.pull_alls

Returns:

  • (Hash)

    The $pullAll operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 253

def pull_alls
  self["$pullAll"] ||= {}
end

#pull_fieldsArray<String> (private)

Get the names of the fields that need to be pulled.

Examples:

Get the pull fields.

modifiers.pull_fields

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 223

def pull_fields
  @pull_fields ||= {}
end

#pullsHash (private)

Get the $pull operations or initialize a new one.

Examples:

Get the $pull operations.

modifiers.pulls

Returns:

  • (Hash)

    The $pull operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 263

def pulls
  self["$pull"] ||= {}
end

#push(modifications)

Adds push modifiers to the modifiers hash.

Examples:

Add push operations.

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

Parameters:

  • modifications (Hash)

    The push modifiers.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 61

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

#push_conflict?(field) ⇒ true | false (private)

Is the operation going to be a conflict for a $push?

Examples:

Is this a conflict for a push?

modifiers.push_conflict?(field)

Parameters:

  • field (String)

    The field.

Returns:

  • (true | false)

    If this field is a conflict.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 171

def push_conflict?(field)
  name = field.split(".", 2)[0]
  set_fields.has_key?(name) || pull_fields.has_key?(name) ||
    (push_fields.keys.count { |item| item.split('.', 2).first == name } > 1)
end

#push_fieldsArray<String> (private)

Get the names of the fields that need to be pushed.

Examples:

Get the push fields.

modifiers.push_fields

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 233

def push_fields
  @push_fields ||= {}
end

#pushesHash (private)

Get the $push/$each operations or initialize a new one.

Examples:

Get the $push/$each operations.

modifiers.pushes

Returns:

  • (Hash)

    The $push/$each operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 273

def pushes
  self["$push"] ||= {}
end

#set(modifications)

Adds set operations to the modifiers hash.

Examples:

Add set operations.

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

Parameters:

  • modifications (Hash)

    The set modifiers.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 75

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

#set_conflict?(field) ⇒ true | false (private)

Is the operation going to be a conflict for a $set?

Examples:

Is this a conflict for a set?

modifiers.set_conflict?(field)

Parameters:

  • field (String)

    The field.

Returns:

  • (true | false)

    If this field is a conflict.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 158

def set_conflict?(field)
  name = field.split(".", 2)[0]
  pull_fields.has_key?(name) || push_fields.has_key?(name)
end

#set_fieldsArray<String> (private)

Get the names of the fields that need to be set.

Examples:

Get the set fields.

modifiers.set_fields

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 243

def set_fields
  @set_fields ||= {}
end

#setsHash (private)

Get the $set operations or initialize a new one.

Examples:

Get the $set operations.

modifiers.sets

Returns:

  • (Hash)

    The $set operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 283

def sets
  self["$set"] ||= {}
end

#unset(modifications)

Adds unset operations to the modifiers hash.

Examples:

Add unset operations.

modifiers.unset([ "addresses" ])

Parameters:

  • modifications (Array<String>)

    The unset association names.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 90

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

#unsetsHash (private)

Get the $unset operations or initialize a new one.

Examples:

Get the $unset operations.

modifiers.unsets

Returns:

  • (Hash)

    The $unset operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic/modifiers.rb', line 293

def unsets
  self["$unset"] ||= {}
end