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.
- <<
- aupdate
- close
- commit
- count
- delete
- delete_all
- enchant
- exist?
- find
- force_save!
- insert
- load
- new
- reload
- rollback
- save
- start
- transaction
- update
- update_attribute
- update_attributes
- validate_and_save
| [RW] | ogmanager |
Create a session to the store.
Default options:
:evolve_schema => :warn
[ show source ]
# File lib/og/store.rb, line 18
18: def initialize options
19: @options = {
20: :evolve_schema => :warn
21: }.merge(options)
22: @transaction_nesting = 0
23: end
Close the session to the store.
[ show source ]
# File lib/og/store.rb, line 27
27: def close
28: end
Enchants a class.
[ show source ]
# File lib/og/store.rb, line 32
32: def enchant(klass, manager)
33: pk = klass.primary_key
34: klass.module_eval %{
35:
36: # A managed object is considered saved if it has
37: # a valid primary key.
38:
39: def saved?
40: return #{pk}
41: end
42:
43: # The inverse of saved.
44:
45: def unsaved?
46: return !#{pk}
47: end
48:
49: # Evaluate an alias for the primary key.
50:
51: alias_method :pk, :#{pk}
52: alias_method :pk=, :#{pk}=
53: }
54: end
Alias for #save
Alias for #update_attributes
Count the results returned by the query.
[ show source ]
# File lib/og/store.rb, line 159
159: def count(options)
160: raise "Not implemented"
161: end
Permanently delete an object from the store.
[ show source ]
# File lib/og/store.rb, line 127
127: def delete(obj_or_pk, klass = nil, cascade = true)
128: unless obj_or_pk.is_a? ModelMixin
129: # create an instance to keep the og_delete
130: # method as an instance method like the other lifecycle
131: # methods. This even allows to call og_delete aspects
132: # that use instance variable (for example, sophisticated
133: # cache sweepers).
134: #
135: # gmosx: the following is not enough!
136: # obj = klass.allocate
137: # obj.pk = obj_or_pk
138: obj = klass[obj_or_pk]
139: obj.og_delete(self, cascade)
140: else
141: obj_or_pk.og_delete(self, cascade)
142: end
143: end
Delete all instances of the given class.
[ show source ]
# File lib/og/store.rb, line 147
147: def delete_all(klass)
148: raise "Not implemented"
149: end
Alias for #load
Perform a query.
[ show source ]
# File lib/og/store.rb, line 153
153: def find(klass, options)
154: raise "Not implemented"
155: end
Force the persistence of an Object. Ignore any validation and/or other errors.
[ show source ]
# File lib/og/store.rb, line 96
96: def force_save!(obj, options)
97: if obj.saved?
98: obj.og_update self, options
99: else
100: obj.og_insert self
101: end
102: end
Insert an object in the store.
[ show source ]
# File lib/og/store.rb, line 106
106: def insert(obj)
107: obj.og_insert self
108: end
Loads an object from the store using the primary key.
[ show source ]
# File lib/og/store.rb, line 60
60: def load(pk, klass)
61: raise 'Not implemented'
62: end
Reloads an object from the store.
[ show source ]
# File lib/og/store.rb, line 67
67: def reload(obj)
68: raise 'Not implemented'
69: end
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.
[ show source ]
# File lib/og/store.rb, line 76
76: def save(obj, options = nil)
77: return false unless obj.valid?
78:
79: if obj.saved?
80: update_count = obj.og_update(self, options)
81: else
82: update_count = 1 if obj.og_insert(self)
83: end
84:
85: # Save building collections if any.
86: obj.save_building_collections
87:
88: return update_count
89: end
Update an object in the store.
[ show source ]
# File lib/og/store.rb, line 112
112: def update(obj, options = nil)
113: obj.og_update self, options
114: end
Alias for #update_attributes
Update selected attributes of an object or class of objects.
[ show source ]
# File lib/og/store.rb, line 119
119: def update_attributes(target, *attributes)
120: update(target, :only => attributes)
121: end
Alias for #save
Commit a transaction.
[ show source ]
# File lib/og/store.rb, line 175
175: def commit
176: raise "Not implemented"
177: @transaction_nesting -= 1
178: true if @transaction_nesting < 1
179: end
Rollback a transaction.
[ show source ]
# File lib/og/store.rb, line 183
183: def rollback
184: @transaction_nesting -= 1
185: true if @transaction_nesting < 1
186: end
Start a new transaction.
[ show source ]
# File lib/og/store.rb, line 167
167: def start
168: raise "Not implemented"
169: true if @transaction_nesting < 1
170: @transaction_nesting += 1
171: end
Transaction helper. In the transaction block use the db pointer to the backend.
[ show source ]
# File lib/og/store.rb, line 191
191: def transaction(&block)
192: start
193: yield self
194: rescue Object => ex
195: rollback
196: else
197: commit
198: end