Class: Og::FilesysStore

Inherits:
Store show all
Defined in:
lib/og/store/alpha/filesys.rb

Overview

A Store that saves the object on the Filesystem. An extension of the Memory store.

Instance Attribute Summary

Attributes inherited from Store

#options, #transaction_nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#close, #commit, for_name, #insert, #rollback, #start, #transaction, #update, #update_properties

Constructor Details

#initialize(options) ⇒ FilesysStore

Initialize a connection to this store.



35
36
37
38
39
40
41
42
43
44
# File 'lib/og/store/alpha/filesys.rb', line 35

def initialize(options)
  super

  @base_dir = self.class.dir(options[:name])

  unless File.directory?(@base_dir)
    Logger.info "Database '#{options[:name]}' not found!"
    self.class.create(options)
  end
end

Class Method Details

.create(options) ⇒ Object



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

def self.create(options)
  name = dir(options[:name])
  FileUtils.mkdir_p(name)
rescue 
  Logger.error "Cannot create '#{name}'"
end

.destroy(options) ⇒ Object



26
27
28
29
30
31
# File 'lib/og/store/alpha/filesys.rb', line 26

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

.dir(name) ⇒ Object

:section: Store methods.



15
16
17
# File 'lib/og/store/alpha/filesys.rb', line 15

def self.dir(name)
  "#{name}_db"
end

Instance Method Details

#countObject



89
90
# File 'lib/og/store/alpha/filesys.rb', line 89

def count
end

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



80
81
82
83
84
# File 'lib/og/store/alpha/filesys.rb', line 80

def delete(obj_or_pk, klass = nil, cascade = true)
  pk = obj_or_pk.is_a?(Fixnum) ? obj_or_pk : obj_or_pk.oid
  klass ||= obj_or_pk.class
  FileUtils.rm(path(klass, pk))
end

#enchant(klass, manager) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/og/store/alpha/filesys.rb', line 46

def enchant(klass, manager)    
  super 

  klass.const_set 'OGNAME', klass.to_s.gsub(/::/, "/").downcase
  klass.property :oid, Fixnum
 
  FileUtils.mkdir_p(path(klass))
  File.open(spath(klass), 'w') { |f| f << '1' }
rescue => ex
  Logger.error "Cannot create directory to store '#{klass}' classes!"                      
end

#findObject



86
87
# File 'lib/og/store/alpha/filesys.rb', line 86

def find
end

#load(oid, klass) ⇒ Object

:section: Lifecycle methods.



60
61
62
63
64
65
66
# File 'lib/og/store/alpha/filesys.rb', line 60

def load(oid, klass)
  obj = nil
  File.open(path(klass, oid), 'r') { |f| obj = YAML::load(f.read) }
  return obj            
rescue
  return nil
end

#reload(obj) ⇒ Object



68
69
# File 'lib/og/store/alpha/filesys.rb', line 68

def reload(obj)
end

#save(obj) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/og/store/alpha/filesys.rb', line 71

def save(obj)
  seq = nil
  File.open(spath(obj.class), 'r') { |f| seq = f.read.to_i }
  obj.oid = seq
  File.open(path(obj.class, obj.oid), 'w') { |f| f << obj.to_yaml }
  seq += 1
  File.open(spath(obj.class), 'w') { |f| f << seq }                        
end