Class: Og::MemoryStore

Inherits:
Store
  • Object
show all
Extended by:
MemoryUtils
Includes:
MemoryUtils
Defined in:
lib/og/store/memory.rb

Overview

A Store that ‘persists’ objects into (RAM) memory. – TODO: fully working query. TODO: arbitrary pk. ++

Defined Under Namespace

Classes: ObjectHash

Instance Attribute Summary collapse

Attributes inherited from Store

#options, #transaction_nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MemoryUtils

join_table, quote, table

Methods inherited from Store

create, #delete, for_name, #insert, #save, #transaction, #update

Constructor Details

#initialize(options) ⇒ MemoryStore

Returns a new instance of MemoryStore.



70
71
72
73
74
75
76
77
# File 'lib/og/store/memory.rb', line 70

def initialize(options)
	super
	begin
		@conn = self.class.objects = YAML.load_file("#{@options[:name]}.db")
	rescue
		@conn = self.class.objects = ObjectHash.new
	end
end

Instance Attribute Details

#connObject

A pseudo-connection to the actual object store.



62
63
64
# File 'lib/og/store/memory.rb', line 62

def conn
  @conn
end

Class Method Details

.destroy(options) ⇒ Object



64
65
66
67
68
# File 'lib/og/store/memory.rb', line 64

def self.destroy(options)
	FileUtils.rm_rf("#{options[:name]}.db")
rescue 
	Logger.info "Cannot destroy '#{name}'"
end

.objectsObject



52
53
54
# File 'lib/og/store/memory.rb', line 52

def self.objects
	@objects
end

.objects=(val) ⇒ Object



56
57
58
# File 'lib/og/store/memory.rb', line 56

def self.objects=(val)
	@objects = val
end

Instance Method Details

#closeObject



79
80
81
# File 'lib/og/store/memory.rb', line 79

def close
	File.open("#{@options[:name]}.db", 'w') { |f| f << @conn.to_yaml }
end

#commitObject

Commit a transaction.



218
219
# File 'lib/og/store/memory.rb', line 218

def commit
end

#count(options) ⇒ Object

Count results.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/og/store/memory.rb', line 157

def count(options)
	objects = 0
	
	if condition = options[:condition] || options[:where]
		condition = "obj." + condition.gsub(/=/, '==')
	else
		condition = true
	end
	
	eval %{
		for obj in @conn[options[:class]].values
			objects += 1 if #{condition}
		end
	}		
	
	return objects
end

#enchant(klass, manager) ⇒ Object

Enchants a class.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/og/store/memory.rb', line 85

def enchant(klass, manager)
	klass.property :oid, Fixnum

	super
	
	@conn[klass] ||= {}
	
	eval_og_insert(klass)
	eval_og_update(klass)
	eval_og_read(klass)
	eval_og_delete(klass)
end

#find(options) ⇒ Object

Find a collection of objects.

Examples

User.find(:condition => ‘age > 15’, :order => ‘score ASC’, :offet => 10, :limit =>10) Comment.find(:include => :entry) store.find(:class => User, :where => ‘age > 15’)



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

def find(options)	
	query(options)
end

#find_one(options) ⇒ Object

Find one object.



145
146
147
# File 'lib/og/store/memory.rb', line 145

def find_one(options)
	query(options).first
end

#join(obj1, obj2, table) ⇒ Object

Relate two objects through an intermediate join table. Typically used in joins_many and many_to_many relations.



178
179
180
# File 'lib/og/store/memory.rb', line 178

def join(obj1, obj2, table)
	# nop
end

#load(pk, klass) ⇒ Object

Loads an object from the store using the primary key.



102
103
104
# File 'lib/og/store/memory.rb', line 102

def load(pk, klass)
	@conn[klass][pk]
end

#query(options) ⇒ Object

Query.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/og/store/memory.rb', line 184

def query(options)
	objects = []
	
	if condition = options[:condition] || options[:where]
		condition = "obj." + condition.gsub(/=/, '==')
	else
		condition = true
	end
	
	eval %{
		for obj in @conn[options[:class]].values
			objects << obj if #{condition}
		end
	}		

	if order = options[:order]
		desc = (order =~ /DESC/)
		order = order.gsub(/DESC/, '').gsub(/ASC/, '')
		eval "objects.sort { |x, y| x.#{order} <=> y.#{order} }"
		objects.reverse! if desc
	end
			
	return objects		
end

#reload(obj, pk) ⇒ Object

Reloads an object from the store.



151
152
153
# File 'lib/og/store/memory.rb', line 151

def reload(obj, pk)
	# Nop, the memory store is always synchronized.
end

#rollbackObject

Rollback a transaction.



223
224
# File 'lib/og/store/memory.rb', line 223

def rollback
end

#startObject

Start a new transaction.



213
214
# File 'lib/og/store/memory.rb', line 213

def start
end

#update_properties(target, set, options = nil) ⇒ Object Also known as: pupdate, update_property

Update selected properties of an object or class of objects.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/og/store/memory.rb', line 109

def update_properties(target, set, options = nil)
	set = set.gsub(/,/, ';')
	if target.is_a?(Class)
		if options
			condition = options[:condition] || options[:where]
		else 
			condition = 'true'
		end
		for obj in @conn[target].values
			obj.instance_eval %{
				if #{condition.gsub(/=/, '==')}
					#{set}
				end
			}
		end
	else
		target.instance_eval(set)
	end			
end