Module: Mongoid::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- Processing, Readonly
- Included in:
- Components
- Defined in:
- lib/mongoid/attributes.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, 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, options = {}) ⇒ 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.
-
#has_attribute?(name) ⇒ true, false
Does the document have the provided attribute?.
-
#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?(name, include_private = false) ⇒ 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 Readonly
Methods included from Processing
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
Used for allowing accessor methods for dynamic attributes.
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/mongoid/attributes.rb', line 223 def method_missing(name, *args) attr = name.to_s return super unless attributes.has_key?(attr.reader) if attr.writer? getter = attr.reader define_dynamic_writer(getter) write_attribute(getter, args.first) else getter = attr.reader define_dynamic_reader(getter) read_attribute(getter) end end |
Instance Attribute Details
#attributes ⇒ Object (readonly) Also known as: raw_attributes
Returns the value of attribute attributes.
14 15 16 |
# File 'lib/mongoid/attributes.rb', line 14 def attributes @attributes end |
Instance Method Details
#assign_attributes(attrs = nil, options = {}) ⇒ 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.
156 157 158 159 160 |
# File 'lib/mongoid/attributes.rb', line 156 def assign_attributes(attrs = nil, = {}) _assigning do process_attributes(attrs, [:as] || :default, ![:without_protection]) end end |
#attribute_present?(name) ⇒ true, false
Determine if an attribute is present.
27 28 29 30 |
# File 'lib/mongoid/attributes.rb', line 27 def attribute_present?(name) attribute = read_attribute(name) !attribute.blank? || attribute == false end |
#has_attribute?(name) ⇒ true, false
Does the document have the provided attribute?
42 43 44 |
# File 'lib/mongoid/attributes.rb', line 42 def has_attribute?(name) attributes.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.
60 61 62 |
# File 'lib/mongoid/attributes.rb', line 60 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.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mongoid/attributes.rb', line 77 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)] = [] attributes.delete(access) end end |
#respond_to?(name, include_private = false) ⇒ true, false
Override respond_to? so it responds properly for dynamic attributes.
99 100 101 102 103 104 105 |
# File 'lib/mongoid/attributes.rb', line 99 def respond_to?(name, include_private = false) super || ( Mongoid.allow_dynamic_fields && attributes && attributes.has_key?(name.to_s.reader) ) 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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongoid/attributes.rb', line 121 def write_attribute(name, value) access = name.to_s if attribute_writable?(access) _assigning do localized = fields[access].try(:localized?) 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, 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.
176 177 178 |
# File 'lib/mongoid/attributes.rb', line 176 def write_attributes(attrs = nil, guard_protected_attributes = true) assign_attributes(attrs, without_protection: !guard_protected_attributes) end |