Module: Mongoid::Attributes::InstanceMethods

Defined in:
lib/mongoid/attributes.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Used for allowing accessor methods for dynamic attributes.



26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/attributes.rb', line 26

def method_missing(name, *args)
  attr = name.to_s
  return super unless @attributes.has_key?(attr.reader)
  if attr.writer?
    # "args.size > 1" allows to simulate 1.8 behavior of "*args"
    @attributes[attr.reader] = (args.size > 1) ? args : args.first
  else
    @attributes[attr.reader]
  end
end

Instance Method Details

#_typeObject

Returns the object type. This corresponds to the name of the class that this Document is, which is used in determining the class to instantiate in various cases.



85
86
87
# File 'lib/mongoid/attributes.rb', line 85

def _type
  @attributes["_type"]
end

#_type=(new_type) ⇒ Object

Set the type of the Document. This should be the name of the class.



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

def _type=(new_type)
  @attributes["_type"] = new_type
end

#idObject Also known as: _id

Get the id associated with this object. This will pull the _id value out of the attributes Hash.



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

def id
  @attributes["_id"]
end

#id=(new_id) ⇒ Object Also known as: _id=

Set the id of the Document to a new one.



18
19
20
# File 'lib/mongoid/attributes.rb', line 18

def id=(new_id)
  @attributes["_id"] = new_id
end

#process(attrs = nil) ⇒ Object

Process the provided attributes casting them to their proper values if a field exists for them on the Document. This will be limited to only the attributes provided in the suppied Hash so that no extra nil values get put into the document’s attributes.



41
42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/attributes.rb', line 41

def process(attrs = nil)
  (attrs || {}).each_pair do |key, value|
    if set_allowed?(key)
      @attributes[key.to_s] = value
    elsif write_allowed?(key)
      send("#{key}=", value)
    end
  end
  setup_modifications
end

#read_attribute(name) ⇒ Object

Read a value from the Document attributes. If the value does not exist it will return nil.

Options:

name: The name of the attribute to get.

Example:

person.read_attribute(:title)



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

def read_attribute(name)
  access = name.to_s
  accessed(access, fields[access].get(@attributes[access]))
end

#remove_attribute(name) ⇒ Object

Remove a value from the Document attributes. If the value does not exist it will fail gracefully.

Options:

name: The name of the attribute to remove.

Example:

person.remove_attribute(:title)



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

def remove_attribute(name)
  access = name.to_s
  modify(access, @attributes.delete(access), nil)
end

#write_attribute(name, value) ⇒ Object

Write a single attribute to the Document attribute Hash. This will also fire the before and after update callbacks, and perform any necessary typecasting.

Options:

name: The name of the attribute to update. value: The value to set for the attribute.

Example:

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

This will also cause the observing Document to notify it’s parent if there is any.



109
110
111
112
113
# File 'lib/mongoid/attributes.rb', line 109

def write_attribute(name, value)
  access = name.to_s
  modify(access, @attributes[access], fields[access].set(value))
  notify if !id.blank? && new_record?
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.

Options:

attrs: The Hash of new attributes to set on the Document

Example:

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

This will also cause the observing Document to notify it’s parent if there is any.



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

def write_attributes(attrs = nil)
  process(attrs || {})
  identified = !id.blank?
  if new_record? && !identified
    identify; notify
  end
end