Class: Og::Store
Overview
A Store is responsible for the peristance of the ObjectGraph.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
Options.
-
#transaction_nesting ⇒ Object
Transaction nesting.
Class Method Summary collapse
-
.create(options) ⇒ Object
Creates a store.
-
.destroy(options) ⇒ Object
Destroys a store.
-
.for_name(name) ⇒ Object
Return the store for the given name.
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.
-
#enchant(klass, manager) ⇒ Object
Enchants a class.
-
#find(klass, options) ⇒ Object
Perform a query.
-
#initialize(options) ⇒ Store
constructor
Create a session to the store.
-
#insert(obj) ⇒ Object
Insert an object in the store.
-
#load(pk, klass) ⇒ Object
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: #<<)
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_properties(obj_or_class, props, options = nil) ⇒ Object
(also: #pupdate, #update_property)
Update selected properties of an object or class of objects.
Constructor Details
#initialize(options) ⇒ Store
Create a session to the store.
41 42 43 44 |
# File 'lib/og/store.rb', line 41 def initialize() @options = @transaction_nesting = 0 end |
Instance Attribute Details
#transaction_nesting ⇒ Object
Transaction nesting.
13 14 15 |
# File 'lib/og/store.rb', line 13 def transaction_nesting @transaction_nesting end |
Class Method Details
.create(options) ⇒ Object
Creates a store.
29 30 |
# File 'lib/og/store.rb', line 29 def self.create() end |
.destroy(options) ⇒ Object
Destroys a store.
34 35 |
# File 'lib/og/store.rb', line 34 def self.destroy() end |
.for_name(name) ⇒ Object
Return the store for the given name.
19 20 21 22 23 24 25 |
# File 'lib/og/store.rb', line 19 def self.for_name(name) # gmosx: to keep RDoc happy. eval %{ require 'og/store/#{name}' return #{name.to_s.capitalize}Store } end |
Instance Method Details
#close ⇒ Object
Close the session to the store.
48 49 |
# File 'lib/og/store.rb', line 48 def close end |
#commit ⇒ Object
Commit a transaction.
160 161 162 163 164 |
# File 'lib/og/store.rb', line 160 def commit raise 'Not implemented' @transaction_nesting -= 1 true if @transaction_nesting < 1 end |
#count(options) ⇒ Object
Count the results returned by the query.
145 146 |
# File 'lib/og/store.rb', line 145 def count() end |
#delete(obj_or_pk, klass = nil, cascade = true) ⇒ Object
Permanently delete an object from the store.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/og/store.rb', line 127 def delete(obj_or_pk, klass = nil, cascade = true) unless obj_or_pk.is_a?(EntityMixin) # create a dummy instance to keep the og_delete # method as an instance method like the other lifecycle # methods. klass.allocate.og_delete(self, obj_or_pk, cascade) else obj_or_pk.og_delete(self, nil, cascade) end end |
#enchant(klass, manager) ⇒ Object
Enchants a class.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/og/store.rb', line 53 def enchant(klass, manager) klass.class.send(:define_method, :index) do |arg| :index, arg end pk = klass.primary_key.symbol klass.module_eval %{ def saved? return #{klass.primary_key.symbol} end def unsaved? return !#{klass.primary_key.symbol} end # Evaluate an alias for the primary key. alias_method :pk, :#{pk} alias_method :pk=, :#{pk}= def self.pk_symbol :#{klass.primary_key.symbol} end } end |
#find(klass, options) ⇒ Object
Perform a query.
140 141 |
# File 'lib/og/store.rb', line 140 def find(klass, ) 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
Loads an object from the store using the primary key.
85 86 |
# File 'lib/og/store.rb', line 85 def load(pk, klass) end |
#reload(obj) ⇒ Object
Reloads an object from the store.
90 91 |
# File 'lib/og/store.rb', line 90 def reload(obj) end |
#rollback ⇒ Object
Rollback a transaction.
168 169 170 171 |
# File 'lib/og/store.rb', line 168 def rollback @transaction_nesting -= 1 true if @transaction_nesting < 1 end |
#save(obj, options = nil) ⇒ Object Also known as: <<
Save an object to store. Insert if this is a new object or update if this is already inserted in the database.
96 97 98 99 100 101 102 |
# File 'lib/og/store.rb', line 96 def save(obj, = nil) if obj.saved? obj.og_update(self, ) else obj.og_insert(self) end end |
#start ⇒ Object
Start a new transaction.
152 153 154 155 156 |
# File 'lib/og/store.rb', line 152 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.
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/og/store.rb', line 176 def transaction(&block) begin start yield(self) commit rescue => ex Logger.error 'Error in transaction' Logger.error ex Logger.error ex.backtrace rollback 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_properties(obj_or_class, props, options = nil) ⇒ Object Also known as: pupdate, update_property
Update selected properties of an object or class of objects.
120 121 |
# File 'lib/og/store.rb', line 120 def update_properties(obj_or_class, props, = nil) end |