Module: Humanoid::Document::InstanceMethods
- Defined in:
- lib/humanoid/document.rb
Instance Method Summary collapse
-
#==(other) ⇒ Object
Performs equality checking on the attributes.
-
#_root ⇒ Object
Return the root
Document
in the object graph. -
#assimilate(parent, options) ⇒ Object
Introduces a child object into the
Document
object graph. -
#attributes ⇒ Object
Return the attributes hash with indifferent access.
-
#clone ⇒ Object
Clone the current
Document
. -
#eql?(comparison_object) ⇒ Boolean
Delegates to ==.
-
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like: [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ].
-
#identify ⇒ Object
Generate an id for this
Document
. -
#initialize(attrs = nil) ⇒ Object
Instantiate a new
Document
, setting the Document’s attributes if given. -
#inspect ⇒ Object
Returns the class name plus its attributes.
-
#new_record=(saved) ⇒ Object
Sets the new_record boolean - used after document is saved.
-
#new_record? ⇒ Boolean
Returns true is the
Document
has not been persisted to the database, false if it has. -
#notify ⇒ Object
Set the changed state of the
Document
then notify observers that it has changed. -
#parentize(object, association_name) ⇒ Object
Sets up a child/parent association.
-
#raw_attributes ⇒ Object
Return the attributes hash.
-
#reload ⇒ Object
Reloads the
Document
attributes from the database. -
#remove(child) ⇒ Object
Remove a child document from this parent
Document
. -
#to_a ⇒ Object
Return an array with this
Document
only in it. -
#to_json(options = nil) ⇒ Object
Return this document as a JSON string.
-
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
-
#update(child, clear = false) ⇒ Object
Observe a notify call from a child
Document
.
Instance Method Details
#==(other) ⇒ Object
Performs equality checking on the attributes. For now we chack against all attributes excluding timestamps on the object.
110 111 112 113 114 |
# File 'lib/humanoid/document.rb', line 110 def ==(other) return false unless other.is_a?(Document) attributes.except(:modified_at).except(:created_at) == other.attributes.except(:modified_at).except(:created_at) end |
#_root ⇒ Object
Return the root Document
in the object graph. If the current Document
is the root object in the graph it will return self.
245 246 247 248 249 |
# File 'lib/humanoid/document.rb', line 245 def _root object = self while (object._parent) do object = object._parent; end object || self end |
#assimilate(parent, options) ⇒ Object
Introduces a child object into the Document
object graph. This will set up the relationships between the parent and child and update the attributes of the parent Document
.
Options:
parent: The Document
to assimilate with. options: The association Options
for the child.
Example:
name.assimilate(person, options)
139 140 141 |
# File 'lib/humanoid/document.rb', line 139 def assimilate(parent, ) parentize(parent, .name); notify; self end |
#attributes ⇒ Object
Return the attributes hash with indifferent access.
144 145 146 |
# File 'lib/humanoid/document.rb', line 144 def attributes @attributes.with_indifferent_access end |
#clone ⇒ Object
Clone the current Document
. This will return all attributes with the exception of the document’s id and versions.
150 151 152 |
# File 'lib/humanoid/document.rb', line 150 def clone self.class.instantiate(@attributes.except("_id").except("versions").dup, true) end |
#eql?(comparison_object) ⇒ Boolean
Delegates to ==
117 118 119 |
# File 'lib/humanoid/document.rb', line 117 def eql?(comparison_object) self == (comparison_object) end |
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like:
[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
123 124 125 |
# File 'lib/humanoid/document.rb', line 123 def hash id.hash end |
#identify ⇒ Object
Generate an id for this Document
.
155 156 157 |
# File 'lib/humanoid/document.rb', line 155 def identify Identity.create(self) end |
#initialize(attrs = nil) ⇒ Object
Instantiate a new Document
, setting the Document’s attributes if given. If no attributes are provided, they will be initialized with an empty Hash
.
If a primary key is defined, the document’s id will be set to that key, otherwise it will be set to a fresh Mongo::ObjectID
string.
Options:
attrs: The attributes Hash
to set up the document with.
169 170 171 172 173 174 175 176 |
# File 'lib/humanoid/document.rb', line 169 def initialize(attrs = nil) @attributes = {} process(attrs) @attributes = attributes_with_defaults(@attributes) @new_record = true if id.nil? document = yield self if block_given? identify end |
#inspect ⇒ Object
Returns the class name plus its attributes.
179 180 181 182 |
# File 'lib/humanoid/document.rb', line 179 def inspect attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" } * ", " "#<#{self.class.name} _id: #{id}, #{attrs}>" end |
#new_record=(saved) ⇒ Object
Sets the new_record boolean - used after document is saved.
192 193 194 |
# File 'lib/humanoid/document.rb', line 192 def new_record=(saved) @new_record = saved end |
#new_record? ⇒ Boolean
Returns true is the Document
has not been persisted to the database, false if it has. This is determined by the variable @new_record and NOT if the object has an id.
187 188 189 |
# File 'lib/humanoid/document.rb', line 187 def new_record? @new_record == true end |
#notify ⇒ Object
Set the changed state of the Document
then notify observers that it has changed.
Example:
person.notify
201 202 203 204 |
# File 'lib/humanoid/document.rb', line 201 def notify changed(true) notify_observers(self) end |
#parentize(object, association_name) ⇒ Object
Sets up a child/parent association. This is used for newly created objects so they can be properly added to the graph and have the parent observers set up properly.
Options:
abject: The parent object that needs to be set for the child. association_name: The name of the association for the child.
Example:
address.parentize(person, :addresses)
218 219 220 221 222 |
# File 'lib/humanoid/document.rb', line 218 def parentize(object, association_name) self._parent = object self.association_name = association_name.to_s add_observer(object) end |
#raw_attributes ⇒ Object
Return the attributes hash.
225 226 227 |
# File 'lib/humanoid/document.rb', line 225 def raw_attributes @attributes end |
#reload ⇒ Object
Reloads the Document
attributes from the database.
230 231 232 233 |
# File 'lib/humanoid/document.rb', line 230 def reload @attributes = collection.find_one(:_id => id) self end |
#remove(child) ⇒ Object
Remove a child document from this parent Document
. Will reset the memoized association and notify the parent of the change.
237 238 239 240 241 |
# File 'lib/humanoid/document.rb', line 237 def remove(child) name = child.association_name reset(name) { @attributes.remove(name, child.raw_attributes) } notify end |
#to_a ⇒ Object
Return an array with this Document
only in it.
252 253 254 |
# File 'lib/humanoid/document.rb', line 252 def to_a [ self ] end |
#to_json(options = nil) ⇒ Object
Return this document as a JSON string. Nothing special is required here since Humanoid bubbles up all the child associations to the parent attribute Hash
using observers throughout the Document
lifecycle.
Example:
person.to_json
263 264 265 |
# File 'lib/humanoid/document.rb', line 263 def to_json( = nil) attributes.to_json() end |
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
268 269 270 |
# File 'lib/humanoid/document.rb', line 268 def to_param id end |
#update(child, clear = false) ⇒ Object
Observe a notify call from a child Document
. This will either update existing attributes on the Document
or clear them out for the child if the clear boolean is provided.
Options:
child: The child Document
that sent the notification. clear: Will clear out the child’s attributes if set to true.
This will also cause the observing Document
to notify it’s parent if there is any.
283 284 285 286 287 288 |
# File 'lib/humanoid/document.rb', line 283 def update(child, clear = false) name = child.association_name attrs = child.instance_variable_get(:@attributes) clear ? @attributes.delete(name) : @attributes.insert(name, attrs) notify end |