Module: Mongoid::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- Nested, Processing, Readonly
- Included in:
- Composable
- Defined in:
- lib/mongoid/attributes.rb,
lib/mongoid/attributes/nested.rb,
lib/mongoid/attributes/dynamic.rb,
lib/mongoid/attributes/readonly.rb,
lib/mongoid/attributes/processing.rb
Overview
This module contains the logic for handling the internal attributes hash, and how to get and set values.
Defined Under Namespace
Modules: ClassMethods, Dynamic, Nested, Processing, Readonly
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #raw_attributes)
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#assign_attributes(attrs = nil) ⇒ Object
Allows you to set all the attributes for a particular mass-assignment security role by passing in a hash of attributes with keys matching the attribute names (which again matches the column names) and the role name using the :as option.
-
#attribute_present?(name) ⇒ true, false
Determine if an attribute is present.
-
#attributes_before_type_cast ⇒ Hash
Get the attributes that have not been cast.
-
#has_attribute?(name) ⇒ true, false
Does the document have the provided attribute?.
-
#has_attribute_before_type_cast?(name) ⇒ true, false
Does the document have the provided attribute before it was assigned and type cast?.
-
#read_attribute(name) ⇒ Object
(also: #[])
Read a value from the document attributes.
-
#read_attribute_before_type_cast(name) ⇒ Object
Read a value from the attributes before type cast.
-
#remove_attribute(name) ⇒ Object
Remove a value from the
Document
attributes. -
#write_attribute(name, value) ⇒ Object
(also: #[]=)
Write a single attribute to the document attribute hash.
-
#write_attributes(attrs = nil) ⇒ Object
(also: #attributes=)
Writes the supplied attributes hash to the document.
Methods included from Readonly
Methods included from Processing
Instance Attribute Details
#attributes ⇒ Object (readonly) Also known as: raw_attributes
Returns the value of attribute attributes.
17 18 19 |
# File 'lib/mongoid/attributes.rb', line 17 def attributes @attributes end |
Instance Method Details
#assign_attributes(attrs = nil) ⇒ Object
Allows you to set all the attributes for a particular mass-assignment security role by passing in a hash of attributes with keys matching the attribute names (which again matches the column names) and the role name using the :as option. To bypass mass-assignment security you can use the :without_protection => true option.
196 197 198 199 200 |
# File 'lib/mongoid/attributes.rb', line 196 def assign_attributes(attrs = nil) _assigning do process_attributes(attrs) end end |
#attribute_present?(name) ⇒ true, false
Determine if an attribute is present.
30 31 32 33 |
# File 'lib/mongoid/attributes.rb', line 30 def attribute_present?(name) attribute = read_attribute(name) !attribute.blank? || attribute == false end |
#attributes_before_type_cast ⇒ Hash
Get the attributes that have not been cast.
43 44 45 |
# File 'lib/mongoid/attributes.rb', line 43 def attributes_before_type_cast @attributes_before_type_cast ||= {} end |
#has_attribute?(name) ⇒ true, false
Does the document have the provided attribute?
57 58 59 |
# File 'lib/mongoid/attributes.rb', line 57 def has_attribute?(name) attributes.has_key?(name.to_s) end |
#has_attribute_before_type_cast?(name) ⇒ true, false
Does the document have the provided attribute before it was assigned and type cast?
73 74 75 |
# File 'lib/mongoid/attributes.rb', line 73 def has_attribute_before_type_cast?(name) attributes_before_type_cast.has_key?(name.to_s) 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.
91 92 93 94 95 96 97 98 |
# File 'lib/mongoid/attributes.rb', line 91 def read_attribute(name) normalized = database_field_name(name.to_s) if hash_dot_syntax?(normalized) attributes.__nested__(normalized) else attributes[normalized] end end |
#read_attribute_before_type_cast(name) ⇒ Object
Read a value from the attributes before type cast. If the value has not yet been assigned then this will return the attribute’s existing value using read_attribute.
114 115 116 117 118 119 120 121 |
# File 'lib/mongoid/attributes.rb', line 114 def read_attribute_before_type_cast(name) attr = name.to_s if attributes_before_type_cast.has_key?(attr) attributes_before_type_cast[attr] else read_attribute(attr) end end |
#remove_attribute(name) ⇒ Object
Remove a value from the Document
attributes. If the value does not exist it will fail gracefully.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mongoid/attributes.rb', line 135 def remove_attribute(name) access = name.to_s unless attribute_writable?(name) raise Errors::ReadonlyAttribute.new(name, :nil) end _assigning do attribute_will_change!(access) delayed_atomic_unsets[atomic_attribute_name(access)] = [] unless new_record? attributes.delete(access) end 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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/mongoid/attributes.rb', line 161 def write_attribute(name, value) access = database_field_name(name.to_s) if attribute_writable?(access) _assigning do localized = fields[access].try(:localized?) attributes_before_type_cast[name.to_s] = value typed_value = typed_value_for(access, value) unless attributes[access] == typed_value || attribute_changed?(access) attribute_will_change!(access) end if localized (attributes[access] ||= {}).merge!(typed_value) else attributes[access] = typed_value end typed_value end end end |
#write_attributes(attrs = nil) ⇒ 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.
216 217 218 |
# File 'lib/mongoid/attributes.rb', line 216 def write_attributes(attrs = nil) assign_attributes(attrs) end |