Class: Og::Store
- Inherits:
-
Object
- Object
- Og::Store
- Defined in:
- lib/og/store.rb
Overview
A Store is a backend of Og used to persist objects. An adapter specializes the Store. For example the SQL Store has the MySQL, PostgreSQL, Sqlite3 etc adapters as specializations.
Instance Attribute Summary collapse
-
#ogmanager ⇒ Object
Returns the value of attribute ogmanager.
Instance Method Summary collapse
-
#close ⇒ Object
Close the session to the store.
-
#commit ⇒ Object
Commit a transaction.
-
#count(options) ⇒ Object
Count the results returned by the query.
-
#delete(obj_or_pk, klass = nil, cascade = true) ⇒ Object
Permanently delete an object from the store.
-
#delete_all(klass) ⇒ Object
Delete all instances of the given class.
-
#enchant(klass, manager) ⇒ Object
Enchants a class.
-
#find(klass, options) ⇒ Object
Perform a query.
-
#force_save!(obj, options) ⇒ Object
Force the persistence of an Object.
-
#initialize(options) ⇒ Store
constructor
Create a session to the store.
-
#insert(obj) ⇒ Object
Insert an object in the store.
-
#load(pk, klass) ⇒ Object
(also: #exist?)
Loads an object from the store using the primary key.
-
#reload(obj) ⇒ Object
Reloads an object from the store.
-
#rollback ⇒ Object
Rollback a transaction.
-
#save(obj, options = nil) ⇒ Object
(also: #<<, #validate_and_save)
Save an object to store.
-
#start ⇒ Object
Start a new transaction.
-
#transaction(&block) ⇒ Object
Transaction helper.
-
#update(obj, options = nil) ⇒ Object
Update an object in the store.
-
#update_attributes(target, *attributes) ⇒ Object
(also: #aupdate, #update_attribute)
Update selected attributes of an object or class of objects.
Constructor Details
#initialize(options) ⇒ Store
Create a session to the store.
Default options
-
:evolve_schema => :warn
18 19 20 21 22 23 |
# File 'lib/og/store.rb', line 18 def initialize @options = { :evolve_schema => :warn }.merge() @transaction_nesting = 0 end |
Instance Attribute Details
#ogmanager ⇒ Object
Returns the value of attribute ogmanager.
10 11 12 |
# File 'lib/og/store.rb', line 10 def ogmanager @ogmanager end |
Instance Method Details
#close ⇒ Object
Close the session to the store.
27 28 29 |
# File 'lib/og/store.rb', line 27 def close raise 'Not implemented' end |
#commit ⇒ Object
Commit a transaction.
176 177 178 179 180 |
# File 'lib/og/store.rb', line 176 def commit raise 'Not implemented' @transaction_nesting -= 1 true if @transaction_nesting < 1 end |
#count(options) ⇒ Object
Count the results returned by the query.
160 161 162 |
# File 'lib/og/store.rb', line 160 def count() raise 'Not implemented' end |
#delete(obj_or_pk, klass = nil, cascade = true) ⇒ Object
Permanently delete an object from the store.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/og/store.rb', line 128 def delete(obj_or_pk, klass = nil, cascade = true) unless obj_or_pk.is_a? EntityMixin # create an instance to keep the og_delete # method as an instance method like the other lifecycle # methods. This even allows to call og_delete aspects # that use instance variable (for example, sophisticated # cache sweepers). # # gmosx: the following is not enough! # obj = klass.allocate # obj.pk = obj_or_pk obj = klass[obj_or_pk] obj.og_delete(self, cascade) else obj_or_pk.og_delete(self, cascade) end end |
#delete_all(klass) ⇒ Object
Delete all instances of the given class.
148 149 150 |
# File 'lib/og/store.rb', line 148 def delete_all(klass) raise 'Not implemented' end |
#enchant(klass, manager) ⇒ Object
Enchants a class.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/og/store.rb', line 33 def enchant(klass, manager) pk = klass.primary_key klass.module_eval %{ # A managed object is considered saved if it has # a valid primary key. def saved? return #{pk} end # The inverse of saved. def unsaved? return !#{pk} end # Evaluate an alias for the primary key. alias_method :pk, :#{pk} alias_method :pk=, :#{pk}= } end |
#find(klass, options) ⇒ Object
Perform a query.
154 155 156 |
# File 'lib/og/store.rb', line 154 def find(klass, ) raise 'Not implemented' end |
#force_save!(obj, options) ⇒ Object
Force the persistence of an Object. Ignore any validation and/or other errors.
97 98 99 100 101 102 103 |
# File 'lib/og/store.rb', line 97 def force_save!(obj, ) if obj.saved? obj.og_update self, else obj.og_insert self end end |
#insert(obj) ⇒ Object
Insert an object in the store.
107 108 109 |
# File 'lib/og/store.rb', line 107 def insert(obj) obj.og_insert self end |
#load(pk, klass) ⇒ Object Also known as: exist?
Loads an object from the store using the primary key.
61 62 63 |
# File 'lib/og/store.rb', line 61 def load(pk, klass) raise 'Not implemented' end |
#reload(obj) ⇒ Object
Reloads an object from the store.
68 69 70 |
# File 'lib/og/store.rb', line 68 def reload(obj) raise 'Not implemented' end |
#rollback ⇒ Object
Rollback a transaction.
184 185 186 187 |
# File 'lib/og/store.rb', line 184 def rollback @transaction_nesting -= 1 true if @transaction_nesting < 1 end |
#save(obj, options = nil) ⇒ Object Also known as: <<, validate_and_save
Save an object to store. Insert if this is a new object or update if this is already inserted in the database. Checks if the object is valid before saving. Returns false if the object is invalid and populates obj.errors.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/og/store.rb', line 77 def save(obj, = nil) return false unless obj.valid? if obj.saved? update_count = obj.og_update(self, ) else update_count = 1 if obj.og_insert(self) end # Save building collections if any. obj.save_building_collections return update_count end |
#start ⇒ Object
Start a new transaction.
168 169 170 171 172 |
# File 'lib/og/store.rb', line 168 def start raise 'Not implemented' true if @transaction_nesting < 1 @transaction_nesting += 1 end |
#transaction(&block) ⇒ Object
Transaction helper. In the transaction block use the db pointer to the backend.
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/og/store.rb', line 192 def transaction(&block) begin start yield(self) commit rescue Object => ex rollback raise ex end end |
#update(obj, options = nil) ⇒ Object
Update an object in the store.
113 114 115 |
# File 'lib/og/store.rb', line 113 def update obj, = nil obj.og_update self, end |
#update_attributes(target, *attributes) ⇒ Object Also known as: aupdate, update_attribute
Update selected attributes of an object or class of objects.
120 121 122 |
# File 'lib/og/store.rb', line 120 def update_attributes target, *attributes update(target, :only => attributes) end |