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
-
#process_attributes(attrs = nil) {|_self| ... }
Process the provided attributes casting them to their proper values if a field exists for them on the document.
-
#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.
-
#pending_nested ⇒ Hash
private
Get all the pending nested attributes that need to be set.
-
#pending_relations ⇒ Hash
private
Get all the pending associations that need to be set.
-
#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.
-
#process_nested
private
Process all the pending nested attributes that needed to wait until ids were set to fire off.
-
#process_pending
private
Process all the pending items, then clear them out.
-
#process_relations
private
Process all the pending associations that needed to wait until ids were set to fire off.
-
#set_pending_nested(name, aliased, value)
private
::Set
value of the pending nested attribute. -
#set_pending_relation(name, aliased, value)
private
::Set
value of the pending relation.
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.
# 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_nested ⇒ Hash (private)
Get all the pending nested attributes that need to be set.
# File 'lib/mongoid/attributes/processing.rb', line 103
def pending_nested @pending_nested ||= {} end
#pending_relations ⇒ Hash (private)
Get all the pending associations that need to be set.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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