Class: Access::YAMLBase

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/access/yamlbase.rb

Overview

An Access compatible storage backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#join

Constructor Details

#initialize(extender, path = nil) ⇒ YAMLBase

extender: a module that will extend this instance and provide additional

functionality

framework: the Access instance this data-storage is tied to path: the path to the data



25
26
27
28
29
30
31
32
33
# File 'lib/access/yamlbase.rb', line 25

def initialize(extender, path=nil)
	@path      = path
	@extender  = extender
	@data      = {}
	extend(extender)

	@path    ||= default_path
	raise "Path must be a directory (#@path)" unless File.directory?(@path)
end

Instance Attribute Details

#accessObject

Returns the value of attribute access.



19
20
21
# File 'lib/access/yamlbase.rb', line 19

def access
  @access
end

Instance Method Details

#<<(record) ⇒ Object Also known as: add

Add a record to the storage



74
75
76
77
# File 'lib/access/yamlbase.rb', line 74

def <<(record)
	@data[record.id]	= record
	save(record.id)
end

#[](id, force_load = false) ⇒ Object

Retrieve the object with id, if force_load cache will be ignored.



65
66
67
68
69
70
71
# File 'lib/access/yamlbase.rb', line 65

def [](id, force_load=false)
	if force_load then
		@data[id] = load(id)
	else
		@data[id] ||= load(id)
	end
end

#cache_allObject

Loads all entries into cache



55
56
57
58
59
60
61
# File 'lib/access/yamlbase.rb', line 55

def cache_all
	slice = (@path.length+1)..-6
	Dir.glob("#{@path}/*.yaml") { |path| # /**
		id = path[slice]
		@data[id] ||= load(id)
	}
end

#delete(record) ⇒ Object

Delete a record from the storage



81
82
83
84
85
# File 'lib/access/yamlbase.rb', line 81

def delete(record)
	id	= record.kind_of?(@type) ? record.id : record
	@data.delete(id)
	File.delete(filename(id))
end

#eachObject

Iterate over records, yielding id and object



94
95
96
97
98
99
100
# File 'lib/access/yamlbase.rb', line 94

def each
	slice = (@path.length+1)..-6
	Dir.glob("#{@path}/*.yaml") { |path|
		id = path[slice]
		yield(id, @data[id] || load(id))
	}
end

#each_keyObject

Iterate over records, yielding id



103
104
105
106
107
# File 'lib/access/yamlbase.rb', line 103

def each_key
	Dir.glob("#{@path}/*.yaml") { |path|
		yield(path2id(path))
	}
end

#escape(id) ⇒ Object



40
41
42
# File 'lib/access/yamlbase.rb', line 40

def escape(id)
	id.gsub(/[\x00-\x1f.%]/) { |m| "%%%02x"%m }.gsub("/", ".")
end

#exists?(id) ⇒ Boolean Also known as: exist?

Check existency of an object.

Returns:

  • (Boolean)


110
111
112
# File 'lib/access/yamlbase.rb', line 110

def exists?(id)
	@data.has_key?(id) || File.exists?(filename(id))
end

#filename(id) ⇒ Object

The full path and filename to the data object belonging to id



36
37
38
# File 'lib/access/yamlbase.rb', line 36

def filename(id)
	"#{@path}/#{escape(id)}.yaml"
end

#keysObject



87
88
89
90
91
# File 'lib/access/yamlbase.rb', line 87

def keys
	Dir.glob("#{@path}/*.yaml").map { |path|
		path2id(path)
	}
end

#load(id) ⇒ Object

Load an entry, will not write to cache, returns nil if entry doesn’t exist



49
50
51
52
# File 'lib/access/yamlbase.rb', line 49

def load(id)
	file = filename(id)
	File.exist?(file) ? YAML.load_file(file) : nil
end

#path2id(path) ⇒ Object



44
45
46
# File 'lib/access/yamlbase.rb', line 44

def path2id(path)
	path[(@path.length+1)..-6].gsub(".", "/").gsub(/%([\dA-Fa-f]{2})/) { $1.to_i(16).chr }
end

#save(id = nil) ⇒ Object

Synchronize data-cache with filesystem.



116
117
118
119
120
121
122
123
124
# File 'lib/access/yamlbase.rb', line 116

def save(id=nil)
	if @data.has_key?(id) then
		File.open(filename(id), 'w') { |fh| fh.write(@data[id].storable.to_yaml) }
	elsif id.nil? then
		@data.each_key { |key| save(key) }
	else
		raise "Could not save '#{id}' since it's not in @data"
	end
end