Class: Og::MemoryStore
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
-
#conn ⇒ Object
A pseudo-connection to the actual object store.
Attributes inherited from Store
#options, #transaction_nesting
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#commit ⇒ Object
Commit a transaction.
-
#count(options) ⇒ Object
Count results.
-
#enchant(klass, manager) ⇒ Object
Enchants a class.
-
#find(options) ⇒ Object
Find a collection of objects.
-
#find_one(options) ⇒ Object
Find one object.
-
#initialize(options) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
-
#join(obj1, obj2, table) ⇒ Object
Relate two objects through an intermediate join table.
-
#load(pk, klass) ⇒ Object
Loads an object from the store using the primary key.
-
#query(options) ⇒ Object
Query.
-
#reload(obj, pk) ⇒ Object
Reloads an object from the store.
-
#rollback ⇒ Object
Rollback a transaction.
-
#start ⇒ Object
Start a new transaction.
-
#update_properties(target, set, options = nil) ⇒ Object
(also: #pupdate, #update_property)
Update selected properties of an object or class of objects.
Methods included from MemoryUtils
Methods inherited from Store
create, #delete, for_name, #force_save!, #insert, #save, #transaction, #update
Constructor Details
#initialize(options) ⇒ MemoryStore
Returns a new instance of MemoryStore.
72 73 74 75 76 77 78 79 |
# File 'lib/og/store/alpha/memory.rb', line 72 def initialize() super begin @conn = self.class.objects = YAML.load_file("#{@options[:name]}.db") rescue @conn = self.class.objects = ObjectHash.new end end |
Instance Attribute Details
#conn ⇒ Object
A pseudo-connection to the actual object store.
64 65 66 |
# File 'lib/og/store/alpha/memory.rb', line 64 def conn @conn end |
Class Method Details
.destroy(options) ⇒ Object
66 67 68 69 70 |
# File 'lib/og/store/alpha/memory.rb', line 66 def self.destroy() FileUtils.rm_rf("#{[:name]}.db") rescue Logger.info "Cannot destroy '#{name}'" end |
.objects ⇒ Object
54 55 56 |
# File 'lib/og/store/alpha/memory.rb', line 54 def self.objects @objects end |
.objects=(val) ⇒ Object
58 59 60 |
# File 'lib/og/store/alpha/memory.rb', line 58 def self.objects=(val) @objects = val end |
Instance Method Details
#close ⇒ Object
81 82 83 |
# File 'lib/og/store/alpha/memory.rb', line 81 def close File.open("#{@options[:name]}.db", 'w') { |f| f << @conn.to_yaml } end |
#commit ⇒ Object
Commit a transaction.
220 221 |
# File 'lib/og/store/alpha/memory.rb', line 220 def commit end |
#count(options) ⇒ Object
Count results.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/og/store/alpha/memory.rb', line 159 def count() objects = 0 if condition = [:condition] || [: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.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/og/store/alpha/memory.rb', line 87 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’)
141 142 143 |
# File 'lib/og/store/alpha/memory.rb', line 141 def find() query() end |
#find_one(options) ⇒ Object
Find one object.
147 148 149 |
# File 'lib/og/store/alpha/memory.rb', line 147 def find_one() query().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.
180 181 182 |
# File 'lib/og/store/alpha/memory.rb', line 180 def join(obj1, obj2, table) # nop end |
#load(pk, klass) ⇒ Object
Loads an object from the store using the primary key.
104 105 106 |
# File 'lib/og/store/alpha/memory.rb', line 104 def load(pk, klass) @conn[klass][pk] end |
#query(options) ⇒ Object
Query.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/og/store/alpha/memory.rb', line 186 def query() objects = [] if condition = [:condition] || [:where] condition = "obj." + condition.gsub(/=/, '==') else condition = true end eval %{ for obj in @conn[options[:class]].values objects << obj if #{condition} end } if order = [: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.
153 154 155 |
# File 'lib/og/store/alpha/memory.rb', line 153 def reload(obj, pk) # Nop, the memory store is always synchronized. end |
#rollback ⇒ Object
Rollback a transaction.
225 226 |
# File 'lib/og/store/alpha/memory.rb', line 225 def rollback end |
#start ⇒ Object
Start a new transaction.
215 216 |
# File 'lib/og/store/alpha/memory.rb', line 215 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/og/store/alpha/memory.rb', line 111 def update_properties(target, set, = nil) set = set.gsub(/,/, ';') if target.is_a?(Class) if condition = [:condition] || [: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 |