Module: Mongoid::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Processing
Included in:
Components
Defined in:
lib/mongoid/attributes.rb,
lib/mongoid/attributes/processing.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, Processing

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Processing

#process

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.

Parameters:

  • name (String, Symbol)

    The name of the method.

  • *args (Array)

    The arguments to the method.



182
183
184
185
186
187
188
189
190
# File 'lib/mongoid/attributes.rb', line 182

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

#attributesObject (readonly) Also known as: raw_attributes

Returns the value of attribute attributes.



12
13
14
# File 'lib/mongoid/attributes.rb', line 12

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.

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.

  • options (Hash) (defaults to: {})

    Supported options: :without_protection, :as

Since:

  • 2.2.1



131
132
133
134
135
136
137
# File 'lib/mongoid/attributes.rb', line 131

def assign_attributes(attrs = nil, options = {})
  _assigning do
    process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
      document.identify if new? && id.blank?
    end
  end
end

#attribute_present?(name) ⇒ true, false Also known as: has_attribute?

Determine if an attribute is present.

Examples:

Is the attribute present?

person.attribute_present?("title")

Parameters:

Returns:

  • (true, false)

    True if present, false if not.

Since:

  • 1.0.0



25
26
27
28
# File 'lib/mongoid/attributes.rb', line 25

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.

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.

Since:

  • 1.0.0



45
46
47
# File 'lib/mongoid/attributes.rb', line 45

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.

Examples:

Remove the attribute.

person.remove_attribute(:title)

Parameters:

  • name (String, Symbol)

    The name of the attribute to remove.

Since:

  • 1.0.0



59
60
61
62
63
64
65
# File 'lib/mongoid/attributes.rb', line 59

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.

Examples:

Does this object respond to the method?

person.respond_to?(:title)

Parameters:

  • *args (Array)

    The name of the method.

Returns:

  • (true, false)

    True if it does, false if not.

Since:

  • 1.0.0



77
78
79
80
81
82
# File 'lib/mongoid/attributes.rb', line 77

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.

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.

Since:

  • 1.0.0



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongoid/attributes.rb', line 98

def write_attribute(name, value)
  _assigning do
    access = name.to_s
    localized = fields[access].try(:localized?)
    typed_value_for(access, value).tap do |value|
      unless attributes[access] == value || attribute_changed?(access)
        attribute_will_change!(access)
      end
      if localized
        (attributes[access] ||= {}).merge!(value)
      else
        attributes[access] = value
      end
    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.

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.

  • guard_protected_attributes (Boolean) (defaults to: true)

    False to skip mass assignment protection.

Since:

  • 1.0.0



153
154
155
# File 'lib/mongoid/attributes.rb', line 153

def write_attributes(attrs = nil, guard_protected_attributes = true)
  assign_attributes(attrs, :without_protection => !guard_protected_attributes)
end