Module: Og::EntityMixin
Overview
Include this module to classes to make them managable by Og. – gmosx, WARNING: If you change the methods here, don’t forget to update the Cacheable overrides. ++
Class Method Summary collapse
Instance Method Summary collapse
- #assign_attributes(values, options = {}) ⇒ Object (also: #assign)
-
#delete(cascade = true) ⇒ Object
(also: #delete!)
Delete this entity instance from the store.
-
#force_save!(options = nil) ⇒ Object
Force saving of the objects, even if the validations don’t pass.
-
#insert ⇒ Object
Insert the object in the store.
-
#instance_attribute_set(a, val) ⇒ Object
Set attribute (like instance_variable_set).
- #og_quote(obj) ⇒ Object
-
#properties_to_hash ⇒ Object
Returns a symbol => value hash of the object’s properties.
-
#reload ⇒ Object
(also: #reload!)
Reload this entity instance from the store.
-
#save(options = nil) ⇒ Object
(also: #save!, #validate_and_save)
Persist the object.
-
#save_building_collections(options = nil) ⇒ Object
Save all building collections.
-
#saved? ⇒ Boolean
(also: #serialized?)
Is this object saved in the store?.
-
#set_attributes(attrs = {}) ⇒ Object
(also: #set_attribute)
Set attributes (update + save).
- #transaction(&block) ⇒ Object
-
#update(options = nil) ⇒ Object
Update an existing object in the store.
- #update_attributes(*attrs) ⇒ Object (also: #update_attribute, #aupdate, #update_properties, #update_property)
- #update_by_sql(set) ⇒ Object (also: #update_sql, #supdate)
Class Method Details
.included(base) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/og/entity.rb', line 18 def self.included(base) # If the after_enchant callback is defined, call it # to allow for some customization. Have a look at cacheable # for an example. if base.respond_to?(:after_enchant) base.after_enchant(base) end end |
Instance Method Details
#assign_attributes(values, options = {}) ⇒ Object Also known as: assign
129 130 131 132 |
# File 'lib/og/entity.rb', line 129 def assign_attributes(values, = {}) AttributeUtils.populate_object(self, values, ) return self end |
#delete(cascade = true) ⇒ Object Also known as: delete!
Delete this entity instance from the store.
113 114 115 |
# File 'lib/og/entity.rb', line 113 def delete(cascade = true) self.class.ogmanager.store.delete(self, self.class, cascade) end |
#force_save!(options = nil) ⇒ Object
Force saving of the objects, even if the validations don’t pass.
39 40 41 |
# File 'lib/og/entity.rb', line 39 def force_save!( = nil) self.class.ogmanager.store.force_save!(self, ) end |
#insert ⇒ Object
Insert the object in the store.
45 46 47 48 |
# File 'lib/og/entity.rb', line 45 def insert self.class.ogmanager.store.insert(self) return self end |
#instance_attribute_set(a, val) ⇒ Object
Set attribute (like instance_variable_set)
Example
a = Article a.instance_attribute_set :accepted, true
99 100 101 102 |
# File 'lib/og/entity.rb', line 99 def instance_attribute_set(a, val) instance_variable_set "@#{a}", val update_attribute(a.to_sym) end |
#og_quote(obj) ⇒ Object
172 173 174 |
# File 'lib/og/entity.rb', line 172 def og_quote(obj) self.class.ogmanager.store.quote(obj) end |
#properties_to_hash ⇒ Object
Returns a symbol => value hash of the object’s properties.
138 139 140 141 142 143 |
# File 'lib/og/entity.rb', line 138 def properties_to_hash hash = {} for sym, prop in self.class.properties hash[sym] = instance_variable_get("@#{sym}") end end |
#reload ⇒ Object Also known as: reload!
Reload this entity instance from the store.
106 107 108 |
# File 'lib/og/entity.rb', line 106 def reload self.class.ogmanager.store.reload(self, self.pk) end |
#save(options = nil) ⇒ Object Also known as: save!, validate_and_save
Persist the object.
30 31 32 |
# File 'lib/og/entity.rb', line 30 def save( = nil) self.class.ogmanager.store.save(self, ) end |
#save_building_collections(options = nil) ⇒ Object
Save all building collections. Transparently called when saving an object, allows efficient object relationship setup. Example:
a = Article.new a.categories << c1 a.categories << c2 a.tags << t1 a.tags << t2 a.save
– TODO: at the moment, this only handles collection relations. Should handle belongs_to/refers_to as well. ++
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/og/entity.rb', line 161 def save_building_collections( = nil) if @pending_building_collections for rel in self.class.relations next unless rel.class.ann.self.collection? collection = send(rel.name.to_s) collection.save_building_members end @pending_building_collections = false end end |
#saved? ⇒ Boolean Also known as: serialized?
Is this object saved in the store?
124 125 126 |
# File 'lib/og/entity.rb', line 124 def saved? not @oid.nil? end |
#set_attributes(attrs = {}) ⇒ Object Also known as: set_attribute
Set attributes (update + save).
Examples
a = Article a.set_attributes :accepted => true, :update_time => Time.now a.set_attribute :accepted => true
– gmosx, THINK: maybe make this the default behaviour of update_attributes? ++
84 85 86 87 88 89 |
# File 'lib/og/entity.rb', line 84 def set_attributes(attrs = {}) for a, val in attrs instance_variable_set "@#{a}", val end update_attributes(*attrs.keys) end |
#transaction(&block) ⇒ Object
118 119 120 |
# File 'lib/og/entity.rb', line 118 def transaction(&block) self.class.ogmanager.store.transaction(&block) end |
#update(options = nil) ⇒ Object
Update an existing object in the store.
52 53 54 |
# File 'lib/og/entity.rb', line 52 def update( = nil) self.class.ogmanager.store.update(self, ) end |
#update_attributes(*attrs) ⇒ Object Also known as: update_attribute, aupdate, update_properties, update_property
56 57 58 |
# File 'lib/og/entity.rb', line 56 def update_attributes(*attrs) self.class.ogmanager.store.update(self, :only => attrs) end |
#update_by_sql(set) ⇒ Object Also known as: update_sql, supdate
65 66 67 |
# File 'lib/og/entity.rb', line 65 def update_by_sql(set) self.class.ogmanager.store.update_by_sql(self, set) end |