Module: Mongoid::Attributes

Includes:
Processing
Included in:
Components
Defined in:
lib/mongoid/attributes.rb,
lib/mongoid/attributes/processing.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Processing

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.



157
158
159
160
161
162
163
164
165
# File 'lib/mongoid/attributes.rb', line 157

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 Method Details

#attribute_present?(name) ⇒ true, false

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



21
22
23
# File 'lib/mongoid/attributes.rb', line 21

def attribute_present?(name)
  !read_attribute(name).blank?
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



39
40
41
42
43
# File 'lib/mongoid/attributes.rb', line 39

def read_attribute(name)
  access = name.to_s
  value = @attributes[access]
  accessed(access, value)
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



55
56
57
58
# File 'lib/mongoid/attributes.rb', line 55

def remove_attribute(name)
  access = name.to_s
  modify(access, @attributes.delete(access), nil)
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



70
71
72
73
74
75
# File 'lib/mongoid/attributes.rb', line 70

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



91
92
93
94
# File 'lib/mongoid/attributes.rb', line 91

def write_attribute(name, value)
  access = name.to_s
  modify(access, @attributes[access], typed_value_for(access, value))
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



111
112
113
114
115
# File 'lib/mongoid/attributes.rb', line 111

def write_attributes(attrs = nil, guard_protected_attributes = true)
  process(attrs, guard_protected_attributes) do |document|
    document.identify if new? && id.blank?
  end
end