Class: Og::MemoryStore

Inherits:
Store
  • Object
show all
Extended by:
MemoryUtils
Includes:
MemoryUtils
Defined in:
lib/og/store/alpha/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, #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(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.



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(options)
  FileUtils.rm_rf("#{options[:name]}.db")
rescue 
  Logger.info "Cannot destroy '#{name}'"
end

.objectsObject



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

#closeObject



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

#commitObject

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(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.



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

#find_one(options) ⇒ Object

Find one object.



147
148
149
# File 'lib/og/store/alpha/memory.rb', line 147

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.



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(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.



153
154
155
# File 'lib/og/store/alpha/memory.rb', line 153

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

#rollbackObject

Rollback a transaction.



225
226
# File 'lib/og/store/alpha/memory.rb', line 225

def rollback
end

#startObject

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, 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