Class: Og::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/og/store.rb

Overview

A Store is responsible for the peristance of the ObjectGraph.

Direct Known Subclasses

FilesysStore, MemoryStore, SqlStore

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
	@options = options
	@transaction_nesting = 0
end

Instance Attribute Details

#optionsObject

Options.



9
10
11
# File 'lib/og/store.rb', line 9

def options
  @options
end

#transaction_nestingObject

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(options)
end

.destroy(options) ⇒ Object

Destroys a store.



34
35
# File 'lib/og/store.rb', line 34

def self.destroy(options)
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

#closeObject

Close the session to the store.



48
49
# File 'lib/og/store.rb', line 48

def close
end

#commitObject

Commit a transaction.



179
180
181
182
183
# File 'lib/og/store.rb', line 179

def commit
	raise 'Not implemented'
	@transaction_nesting -= 1
	true if @transaction_nesting < 1
end

#count(options) ⇒ Object

Count the results returned by the query.



164
165
# File 'lib/og/store.rb', line 164

def count(options)
end

#delete(obj_or_pk, klass = nil, cascade = true) ⇒ Object

Permanently delete an object from the store.



146
147
148
149
150
151
152
153
154
155
# File 'lib/og/store.rb', line 146

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/og/store.rb', line 53

def enchant(klass, manager)
	klass.class.send(:define_method, :index) do |arg|
		meta :index, arg 
	end

	pk = klass.primary_key.first

	klass.module_eval %{
		def saved?
			return @#{klass.primary_key.first}
		end

		def unsaved?
			return !@#{klass.primary_key.first}
		end

		# Evaluate an alias for the primary key.

		alias_method :pk, :#{pk}
		alias_method :pk=, :#{pk}=
		
		def self.pk_symbol
			:#{klass.primary_key.first}
		end
	}
	
	# Generate finder methods.

	code = ''
	
	for p in klass.properties
		finder = p.meta[:unique] ? 'find_one' : 'find'
		
		code << %{
			def self.find_by_#{p.symbol}(val, operator = '=', options = {})
				options.update(
					:class => #{klass},
					:condition => "#{p.symbol}\#{operator}\#{ogmanager.store.quote(val)}"
				)
				ogmanager.store.#{finder}(options)
			end; 
		}
	end

	klass.module_eval(code)
end

#find(klass, options) ⇒ Object

Perform a query.



159
160
# File 'lib/og/store.rb', line 159

def find(klass, options)
end

#insert(obj) ⇒ Object

Insert an object in the store.



126
127
128
# File 'lib/og/store.rb', line 126

def insert(obj)
	obj.og_insert(self)
end

#load(pk, klass) ⇒ Object

Loads an object from the store using the primary key.



104
105
# File 'lib/og/store.rb', line 104

def load(pk, klass)
end

#reload(obj) ⇒ Object

Reloads an object from the store.



109
110
# File 'lib/og/store.rb', line 109

def reload(obj)
end

#rollbackObject

Rollback a transaction.



187
188
189
190
# File 'lib/og/store.rb', line 187

def rollback
	@transaction_nesting -= 1
	true if @transaction_nesting < 1
end

#save(obj) ⇒ 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.



115
116
117
118
119
120
121
# File 'lib/og/store.rb', line 115

def save(obj)
	if obj.saved?
		obj.og_update(self)
	else
		obj.og_insert(self)
	end
end

#startObject

Start a new transaction.



171
172
173
174
175
# File 'lib/og/store.rb', line 171

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.



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/og/store.rb', line 195

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, properties = nil) ⇒ Object

Update an object in the store.



132
133
134
# File 'lib/og/store.rb', line 132

def update(obj, properties = 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.



139
140
# File 'lib/og/store.rb', line 139

def update_properties(obj_or_class, props, options = nil)
end