123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Attributes::Processing

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/attributes/processing.rb

Overview

This module contains the behavior for processing attributes.

Instance Method Summary

Instance Method Details

#pending_attribute?(key, value) ⇒ true | false (private)

If the key provided is the name of an association or a nested attribute, we need to wait until all other attributes are set before processing these.

Examples:

Is the attribute pending?

document.pending_attribute?(:name, "Durran")

Parameters:

  • key (Symbol)

    The name of the attribute.

  • value (Object)

    The value of the attribute.

Returns:

  • (true | false)

    True if pending, false if not.

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 43

def pending_attribute?(key, value)
  name = key.to_s
  aliased = if aliased_associations.key?(name)
              aliased_associations[name]
            else
              name
            end
  if relations.key?(aliased)
    set_pending_relation(name, aliased, value)
    return true
  end
  if nested_attributes.key?(aliased)
    set_pending_nested(name, aliased, value)
    return true
  end
  false
end

#pending_nestedHash (private)

Get all the pending nested attributes that need to be set.

Examples:

Get the pending nested attributes.

document.pending_nested

Returns:

  • (Hash)

    The pending nested attributes in key/value pairs.

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 103

def pending_nested
  @pending_nested ||= {}
end

#pending_relationsHash (private)

Get all the pending associations that need to be set.

Examples:

Get the pending associations.

document.pending_relations

Returns:

  • (Hash)

    The pending associations in key/value pairs.

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 93

def pending_relations
  @pending_relations ||= {}
end

#process_attribute(name, value) (private)

If the attribute is dynamic, add a field for it with a type of object and then either way set the value.

Examples:

Process the attribute.

document.process_attribute(name, value)

Parameters:

  • name (Symbol)

    The name of the field.

  • value (Object)

    The value of the field.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 115

def process_attribute(name, value)
  if !respond_to?("#{name}=", true) && (store_as = aliased_fields.invert[name.to_s])
    name = store_as
  end
  responds = respond_to?("#{name}=", true)
  raise Errors::UnknownAttribute.new(self.class, name) unless responds

  send("#{name}=", value)
end

#process_attributes(attrs = nil) {|_self| ... }

Process the provided attributes casting them to their proper values if a field exists for them on the document. This will be limited to only the attributes provided in the supplied ::Hash so that no extra nil values get put into the document’s attributes.

Examples:

Process the attributes.

person.process_attributes(:title => "sir", :age => 40)

Parameters:

  • attrs (Hash) (defaults to: nil)

    The attributes to set.

Yields:

  • (_self)

Yield Parameters:

  • _self (Processing)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 16

def process_attributes(attrs = nil)
  attrs ||= {}
  unless attrs.empty?
    attrs = sanitize_for_mass_assignment(attrs)
    attrs.each_pair do |key, value|
      next if pending_attribute?(key, value)

      process_attribute(key, value)
    end
  end
  yield self if block_given?
  process_pending
end

#process_nested (private)

Process all the pending nested attributes that needed to wait until ids were set to fire off.

Examples:

Process the nested attributes.

document.process_nested
[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 130

def process_nested
  pending_nested.each_pair do |name, value|
    send("#{name}=", value)
  end
end

#process_pending (private)

Process all the pending items, then clear them out.

Examples:

Process the pending items.

document.process_pending
[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 140

def process_pending
  process_nested and process_relations
  pending_nested.clear and pending_relations.clear
  _reset_memoized_descendants!
end

#process_relations (private)

Process all the pending associations that needed to wait until ids were set to fire off.

Examples:

Process the associations.

document.process_relations
[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 151

def process_relations
  pending_relations.each_pair do |name, value|
    association = relations[name]
    if value.is_a?(Hash)
      association.nested_builder(value, {}).build(self)
    else
      send("#{name}=", value)
    end
  end
end

#set_pending_nested(name, aliased, value) (private)

::Set value of the pending nested attribute.

Parameters:

  • name (Symbol)

    The name of the nested attribute.

  • aliased (Symbol)

    The aliased name of the nested attribute.

  • value (Object)

    The value of the nested attribute.

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 79

def set_pending_nested(name, aliased, value)
  if stored_as_associations.include?(name)
    pending_nested[aliased] = value
  else
    pending_nested[name] = value
  end
end

#set_pending_relation(name, aliased, value) (private)

::Set value of the pending relation.

Parameters:

  • name (Symbol)

    The name of the relation.

  • aliased (Symbol)

    The aliased name of the relation.

  • value (Object)

    The value of the relation.

[ GitHub ]

  
# File 'lib/mongoid/attributes/processing.rb', line 66

def set_pending_relation(name, aliased, value)
  if stored_as_associations.include?(name)
    pending_relations[aliased] = value
  else
    pending_relations[name] = value
  end
end