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.



40
41
42
43
# File 'lib/og/store.rb', line 40

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.



28
29
# File 'lib/og/store.rb', line 28

def self.create(options)
end

.destroy(options) ⇒ Object

Destroys a store.



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

def self.destroy(options)
end

.for_name(name) ⇒ Object

Return the store for the given name.



19
20
21
22
23
24
# File 'lib/og/store.rb', line 19

def self.for_name(name)
  Logger.info "Og uses the #{name.to_s.capitalize} store."
  # gmosx: to keep RDoc happy.
  require('og/store/' + name.to_s)
  return Og.const_get("#{name.to_s.capitalize}Store")
end

Instance Method Details

#closeObject

Close the session to the store.



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

def close
end

#commitObject

Commit a transaction.



173
174
175
176
177
# File 'lib/og/store.rb', line 173

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

#count(options) ⇒ Object

Count the results returned by the query.



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

def count(options)
end

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

Permanently delete an object from the store.



140
141
142
143
144
145
146
147
148
149
# File 'lib/og/store.rb', line 140

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.



52
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
# File 'lib/og/store.rb', line 52

def enchant(klass, manager)
  # gmosx, FIXME: 
  # ARGH, have to update this!
  #
  # klass.class.send(:define_method, :index) do |arg|
  #   meta :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.



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

def find(klass, options)
end

#force_save!(obj, options) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/og/store.rb', line 110

def force_save!(obj, options)
  if obj.saved?
    obj.og_update(self, options)
  else
    obj.og_insert(self)
  end
end

#insert(obj) ⇒ Object

Insert an object in the store.



120
121
122
# File 'lib/og/store.rb', line 120

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

#load(pk, klass) ⇒ Object

Loads an object from the store using the primary key.



87
88
# File 'lib/og/store.rb', line 87

def load(pk, klass)
end

#reload(obj) ⇒ Object

Reloads an object from the store.



92
93
# File 'lib/og/store.rb', line 92

def reload(obj)
end

#rollbackObject

Rollback a transaction.



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

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

#save(obj, options = nil) ⇒ Object Also known as: <<, validate_and_save

Save an object to store. Insert if this is a new object or update if this is already inserted in the database.



98
99
100
101
102
103
104
105
106
# File 'lib/og/store.rb', line 98

def save(obj, options = nil)
  return false unless obj.valid?

  if obj.saved?
    obj.og_update(self, options)
  else
    obj.og_insert(self)
  end
end

#startObject

Start a new transaction.



165
166
167
168
169
# File 'lib/og/store.rb', line 165

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.



189
190
191
192
193
194
195
196
197
198
# File 'lib/og/store.rb', line 189

def transaction(&block)
  begin
    start
    yield(self) 
    commit
  rescue => ex
    rollback
    raise ex
  end
end

#update(obj, options = nil) ⇒ Object

Update an object in the store.



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

def update(obj, options = nil)
  obj.og_update(self, options)
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.



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

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