Module: Mongoid::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Nested, Processing, Readonly
Included in:
Composable
Defined in:
build/mongoid-master/lib/mongoid/attributes.rb,
build/mongoid-master/lib/mongoid/attributes/nested.rb,
build/mongoid-master/lib/mongoid/attributes/dynamic.rb,
build/mongoid-master/lib/mongoid/attributes/readonly.rb,
build/mongoid-master/lib/mongoid/attributes/projector.rb,
build/mongoid-master/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

Instance Method Summary collapse

Methods included from Readonly

#attribute_writable?

Methods included from Processing

#process_attributes

Instance Attribute Details

#attributesObject (readonly) Also known as: raw_attributes

Returns the value of attribute attributes.



20
21
22
# File 'build/mongoid-master/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.

Examples:

Assign the attributes.

person.assign_attributes(:title => "Mr.")

Assign the attributes (with a role).

person.assign_attributes({ :title => "Mr." }, :as => :admin)

Parameters:

  • attrs (Hash) (defaults to: nil)

    The new attributes to set.



192
193
194
195
196
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 192

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.

Examples:

Is the attribute missing?

document.attribute_missing?("test")

Parameters:

  • name (String)

    The name of the attribute.

Returns:

  • (true, false)

    If the attribute is missing.



223
224
225
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 223

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.

Examples:

Is the attribute present?

person.attribute_present?("title")

Parameters:

  • name (String, Symbol)

    The name of the attribute.

Returns:

  • (true, false)

    True if present, false if not.



31
32
33
34
35
36
# File 'build/mongoid-master/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_castHash

Get the attributes that have not been cast.

Examples:

Get the attributes before type cast.

document.attributes_before_type_cast

Returns:

  • (Hash)

    The uncast attributes.



44
45
46
# File 'build/mongoid-master/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?

Examples:

Does the document have the attribute?

model.has_attribute?(:name)

Parameters:

  • name (String, Symbol)

    The name of the attribute.

Returns:

  • (true, false)

    If the key is present in the attributes.



56
57
58
# File 'build/mongoid-master/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?

Examples:

Does the document have the attribute before it was assigned?

model.has_attribute_before_type_cast?(:name)

Parameters:

  • name (String, Symbol)

    The name of the attribute.

Returns:

  • (true, false)

    If the key is present in the attributes_before_type_cast.



70
71
72
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 70

def has_attribute_before_type_cast?(name)
  attributes_before_type_cast.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.

Examples:

Read an attribute.

person.read_attribute(:title)

Read an attribute (alternate syntax.)

person[:title]

Parameters:

  • name (String, Symbol)

    The name of the attribute to get.

Returns:

  • (Object)

    The value of the attribute.



86
87
88
89
90
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 86

def read_attribute(name)
  field = fields[name.to_s]
  raw = read_raw_attribute(name)
  field ? field.demongoize(raw) : raw
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.

Examples:

Read an attribute before type cast.

person.read_attribute_before_type_cast(:price)

Parameters:

  • name (String, Symbol)

    The name of the attribute to get.

Returns:

  • (Object)

    The value of the attribute before type cast, if available. Otherwise, the value of the attribute.



104
105
106
107
108
109
110
111
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 104

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.

Examples:

Remove the attribute.

person.remove_attribute(:title)

Parameters:

  • name (String, Symbol)

    The name of the attribute to remove.

Raises:



123
124
125
126
127
128
129
130
131
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 123

def remove_attribute(name)
  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_attributesObject

Return type-casted attributes.

Examples:

Type-casted attributes.

document.typed_attributes

Returns:

  • (Object)

    The hash with keys and values of the type-casted attributes.



233
234
235
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 233

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.

Examples:

Write the attribute.

person.write_attribute(:title, "Mr.")

Write the attribute (alternate syntax.)

person[:title] = "Mr."

Parameters:

  • name (String, Symbol)

    The name of the attribute to update.

  • value (Object)

    The value to set for the attribute.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 145

def write_attribute(name, value)
  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
      validate_attribute_value(field_name, value)
      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.

Examples:

Write the attributes.

person.write_attributes(:title => "Mr.")

Write the attributes (alternate syntax.)

person.attributes = { :title => "Mr." }

Parameters:

  • attrs (Hash) (defaults to: nil)

    The new attributes to set.



209
210
211
# File 'build/mongoid-master/lib/mongoid/attributes.rb', line 209

def write_attributes(attrs = nil)
  assign_attributes(attrs)
end