Module: Mongoid::Attributes
- Includes:
- Processing
- Included in:
- Components
- Defined in:
- lib/mongoid/attributes.rb,
lib/mongoid/attributes/processing.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Processing
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #raw_attributes)
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#attribute_present?(name) ⇒ true, false
(also: #has_attribute?)
Determine if an attribute is present.
-
#read_attribute(name) ⇒ Object
(also: #[])
Read a value from the document attributes.
-
#remove_attribute(name) ⇒ Object
Remove a value from the
Document
attributes. -
#respond_to?(*args) ⇒ true, false
Override respond_to? so it responds properly for dynamic attributes.
-
#write_attribute(name, value) ⇒ Object
(also: #[]=)
Write a single attribute to the document attribute hash.
-
#write_attributes(attrs = nil, guard_protected_attributes = true) ⇒ Object
(also: #attributes=)
Writes the supplied attributes hash to the document.
Methods included from Processing
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (protected)
Used for allowing accessor methods for dynamic attributes.
180 181 182 183 184 185 186 187 188 |
# File 'lib/mongoid/attributes.rb', line 180 def method_missing(name, *args) attr = name.to_s return super unless attributes.has_key?(attr.reader) if attr.writer? write_attribute(attr.reader, (args.size > 1) ? args : args.first) else read_attribute(attr.reader) end end |
Instance Attribute Details
#attributes ⇒ Object (readonly) Also known as: raw_attributes
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/mongoid/attributes.rb', line 11 def attributes @attributes end |
Instance Method Details
#attribute_present?(name) ⇒ true, false Also known as: has_attribute?
Determine if an attribute is present.
24 25 26 27 |
# File 'lib/mongoid/attributes.rb', line 24 def attribute_present?(name) attribute = read_attribute(name) ! attribute.blank? || attribute == false end |
#read_attribute(name) ⇒ Object Also known as: []
Read a value from the document attributes. If the value does not exist it will return nil.
44 45 46 |
# File 'lib/mongoid/attributes.rb', line 44 def read_attribute(name) attributes[name.to_s] end |
#remove_attribute(name) ⇒ Object
Remove a value from the Document
attributes. If the value does not exist it will fail gracefully.
58 59 60 61 62 63 64 |
# File 'lib/mongoid/attributes.rb', line 58 def remove_attribute(name) assigning do access = name.to_s attribute_will_change!(access) attributes.delete(access) end end |
#respond_to?(*args) ⇒ true, false
Override respond_to? so it responds properly for dynamic attributes.
76 77 78 79 80 81 |
# File 'lib/mongoid/attributes.rb', line 76 def respond_to?(*args) (Mongoid.allow_dynamic_fields && attributes && attributes.has_key?(args.first.to_s) ) || super end |
#write_attribute(name, value) ⇒ Object Also known as: []=
Write a single attribute to the document attribute hash. This will also fire the before and after update callbacks, and perform any necessary typecasting.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mongoid/attributes.rb', line 97 def write_attribute(name, value) assigning do access = name.to_s typed_value_for(access, value).tap do |value| unless attributes[access] == value || attribute_changed?(access) attribute_will_change!(access) end attributes[access] = value end end end |
#write_attributes(attrs = nil, guard_protected_attributes = true) ⇒ Object Also known as: attributes=
Writes the supplied attributes hash to the document. This will only overwrite existing attributes if they are present in the new Hash
, all others will be preserved.
124 125 126 127 128 129 130 |
# File 'lib/mongoid/attributes.rb', line 124 def write_attributes(attrs = nil, guard_protected_attributes = true) assigning do process(attrs, guard_protected_attributes) do |document| document.identify if new? && id.blank? end end end |