Class: Og::FilesysStore

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

Overview

A Store that saves the object on the Filesystem. You can create multiple instances that point to the same 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.



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

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/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/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/filesys.rb', line 15

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

Instance Method Details

#countObject



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

def count
end

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



83
84
85
86
87
# File 'lib/og/store/filesys.rb', line 83

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/og/store/filesys.rb', line 48

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
	p ex
  Logger.error "Cannot create directory to store '#{klass}' classes!"											
end

#findObject



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

def find
end

#load(oid, klass) ⇒ Object

:section: Lifecycle methods.



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

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



71
72
# File 'lib/og/store/filesys.rb', line 71

def reload(obj)
end

#save(obj) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/og/store/filesys.rb', line 74

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