Module: Humanoid::Attributes::InstanceMethods
- Defined in:
- lib/humanoid/attributes.rb
Instance Method Summary collapse
-
#_type ⇒ Object
Returns the object type.
-
#_type=(new_type) ⇒ Object
Set the type of the
Document
. -
#id ⇒ Object
(also: #_id)
Get the id associated with this object.
-
#id=(new_id) ⇒ Object
(also: #_id=)
Set the id of the
Document
to a new one. -
#method_missing(name, *args) ⇒ Object
Used for allowing accessor methods for dynamic attributes.
-
#process(attrs = nil) ⇒ Object
Process the provided attributes casting them to their proper values if a field exists for them on the
Document
. -
#read_attribute(name) ⇒ Object
Read a value from the
Document
attributes. -
#remove_attribute(name) ⇒ Object
Remove a value from the
Document
attributes. -
#write_attribute(name, value) ⇒ Object
Write a single attribute to the
Document
attributeHash
. -
#write_attributes(attrs = nil) ⇒ Object
(also: #attributes=)
Writes the supplied attributes
Hash
to theDocument
.
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/humanoid/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
#_type ⇒ Object
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.
83 84 85 |
# File 'lib/humanoid/attributes.rb', line 83 def _type @attributes["_type"] end |
#_type=(new_type) ⇒ Object
Set the type of the Document
. This should be the name of the class.
88 89 90 |
# File 'lib/humanoid/attributes.rb', line 88 def _type=(new_type) @attributes["_type"] = new_type end |
#id ⇒ Object 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/humanoid/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/humanoid/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 |
# File 'lib/humanoid/attributes.rb', line 41 def process(attrs = nil) (attrs || {}).each_pair do |key, value| if set_allowed?(key) @attributes[key.to_s] = value else send("#{key}=", value) if value end end 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)
61 62 63 64 |
# File 'lib/humanoid/attributes.rb', line 61 def read_attribute(name) access = name.to_s 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)
76 77 78 |
# File 'lib/humanoid/attributes.rb', line 76 def remove_attribute(name) @attributes.delete(name.to_s) 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.
107 108 109 110 111 |
# File 'lib/humanoid/attributes.rb', line 107 def write_attribute(name, value) access = name.to_s @attributes[access] = fields[access].set(value) notify unless id.blank? 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.
127 128 129 130 131 |
# File 'lib/humanoid/attributes.rb', line 127 def write_attributes(attrs = nil) process(attrs || {}) identify if id.blank? notify end |