Module: Mongoid::Attributes::Processing

Included in:
Mongoid::Attributes
Defined in:
lib/mongoid/attributes/processing.rb

Overview

This module contains the behavior for processing attributes.

Instance Method Summary collapse

Instance Method Details

#process(attrs = nil, role = :default, guard_protected_attributes = true) {|_self| ... } ⇒ Object

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 suppied Hash so that no extra nil values get put into the document’s attributes.

Examples:

Process the attributes.

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

Parameters:

  • attrs (Hash) (defaults to: nil)

    The attributes to set.

  • role (Symbol) (defaults to: :default)

    A role for scoped mass assignment.

  • guard_protected_attributes (Boolean) (defaults to: true)

    False to skip mass assignment protection.

Yields:

  • (_self)

Yield Parameters:

Since:

  • 2.0.0.rc.7



21
22
23
24
25
26
27
28
29
30
# File 'lib/mongoid/attributes/processing.rb', line 21

def process(attrs = nil, role = :default, guard_protected_attributes = true)
  attrs ||= {}
  attrs = sanitize_for_mass_assignment(attrs, role) if guard_protected_attributes
  attrs.each_pair do |key, value|
    next if pending_attribute?(key, value)
    process_attribute(key, value)
  end
  yield self if block_given?
  process_pending
end