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/projector.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 Classes: Projector
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_missing?(name) ⇒ true | false
Determine if the attribute is missing from the document, due to loading it from the database with missing fields.
-
#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?.
-
#process_raw_attribute(name, raw, field) ⇒ Object
private
Process the raw attribute values just read from the documents attributes.
-
#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. -
#typed_attributes ⇒ Object
Return type-casted 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.
20 21 22 |
# File 'lib/mongoid/attributes.rb', line 20 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.
210 211 212 213 214 |
# File 'lib/mongoid/attributes.rb', line 210 def assign_attributes(attrs = nil) _assigning do process_attributes(attrs) end end |
#attribute_missing?(name) ⇒ true | false
Determine if the attribute is missing from the document, due to loading it from the database with missing fields.
241 242 243 |
# File 'lib/mongoid/attributes.rb', line 241 def attribute_missing?(name) !Projector.new(__selected_fields).attribute_or_path_allowed?(name) end |
#attribute_present?(name) ⇒ true | false
Determine if an attribute is present.
31 32 33 34 35 36 |
# File 'lib/mongoid/attributes.rb', line 31 def attribute_present?(name) attribute = read_raw_attribute(name) !attribute.blank? || attribute == false rescue ActiveModel::MissingAttributeError false end |
#attributes_before_type_cast ⇒ Hash
Get the attributes that have not been cast.
44 45 46 |
# File 'lib/mongoid/attributes.rb', line 44 def attributes_before_type_cast @attributes_before_type_cast ||= {} end |
#has_attribute?(name) ⇒ true | false
Does the document have the provided attribute?
56 57 58 |
# File 'lib/mongoid/attributes.rb', line 56 def has_attribute?(name) attributes.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?
70 71 72 |
# File 'lib/mongoid/attributes.rb', line 70 def has_attribute_before_type_cast?(name) attributes_before_type_cast.key?(name.to_s) end |
#process_raw_attribute(name, raw, field) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Process the raw attribute values just read from the documents attributes.
103 104 105 106 107 |
# File 'lib/mongoid/attributes.rb', line 103 def process_raw_attribute(name, raw, field) value = field ? field.demongoize(raw) : raw attribute_will_change!(name) if value.resizable? value 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.
86 87 88 89 90 |
# File 'lib/mongoid/attributes.rb', line 86 def read_attribute(name) field = fields[name.to_s] raw = read_raw_attribute(name) process_raw_attribute(name.to_s, raw, field) 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_raw_attribute.
120 121 122 123 124 125 126 127 |
# File 'lib/mongoid/attributes.rb', line 120 def read_attribute_before_type_cast(name) attr = name.to_s if attributes_before_type_cast.key?(attr) attributes_before_type_cast[attr] else read_raw_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.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mongoid/attributes.rb', line 139 def remove_attribute(name) validate_writable_field_name!(name.to_s) as_writable_attribute!(name) do |access| _assigning do attribute_will_change!(access) delayed_atomic_unsets[atomic_attribute_name(access)] = [] unless new_record? attributes.delete(access) end end end |
#typed_attributes ⇒ Object
Return type-casted attributes.
251 252 253 |
# File 'lib/mongoid/attributes.rb', line 251 def typed_attributes attribute_names.map { |name| [name, send(name)] }.to_h 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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/mongoid/attributes.rb', line 162 def write_attribute(name, value) validate_writable_field_name!(name.to_s) field_name = database_field_name(name) if attribute_missing?(field_name) raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'" end if attribute_writable?(field_name) _assigning do localized = fields[field_name].try(:localized?) attributes_before_type_cast[name.to_s] = value typed_value = typed_value_for(field_name, value) unless attributes[field_name] == typed_value || attribute_changed?(field_name) attribute_will_change!(field_name) end if localized attributes[field_name] ||= {} attributes[field_name].merge!(typed_value) else attributes[field_name] = typed_value end # when writing an attribute, also remove it from the unsets, # so that removing then writing doesn't result in a removal. delayed_atomic_unsets.delete(field_name) typed_value end else # TODO: MONGOID-5072 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.
227 228 229 |
# File 'lib/mongoid/attributes.rb', line 227 def write_attributes(attrs = nil) assign_attributes(attrs) end |