Class: Access::YAMLBase
- Inherits:
-
Object
- Object
- Access::YAMLBase
- Includes:
- Enumerable
- Defined in:
- lib/access/yamlbase.rb
Overview
An Access compatible storage backend
Instance Attribute Summary collapse
-
#access ⇒ Object
Returns the value of attribute access.
Instance Method Summary collapse
-
#<<(record) ⇒ Object
(also: #add)
Add a record to the storage.
-
#[](id, force_load = false) ⇒ Object
Retrieve the object with id, if force_load cache will be ignored.
-
#cache_all ⇒ Object
Loads all entries into cache.
-
#delete(id) ⇒ Object
Delete a record from the storage.
-
#each ⇒ Object
Iterate over records, yielding id and object.
-
#each_key ⇒ Object
Iterate over records, yielding id.
-
#escape(id) ⇒ Object
Escapes path names.
-
#exists?(id) ⇒ Boolean
(also: #exist?)
Check existency of an object.
-
#filename(id) ⇒ Object
The full path and filename to the data object belonging to id.
-
#initialize(extender, path = nil) ⇒ YAMLBase
constructor
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.
-
#keys ⇒ Object
All record id’s.
-
#load(id) ⇒ Object
Load an entry, will not write to cache, returns nil if entry doesn’t exist.
-
#path2id(path) ⇒ Object
Extracts the record-id from a path.
-
#save(id = nil) ⇒ Object
Synchronize data-cache with filesystem.
Methods included from Enumerable
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
26 27 28 29 30 31 32 33 34 |
# File 'lib/access/yamlbase.rb', line 26 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
#access ⇒ Object
Returns the value of attribute access.
20 21 22 |
# File 'lib/access/yamlbase.rb', line 20 def access @access end |
Instance Method Details
#<<(record) ⇒ Object Also known as: add
Add a record to the storage
77 78 79 80 |
# File 'lib/access/yamlbase.rb', line 77 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.
68 69 70 71 72 73 74 |
# File 'lib/access/yamlbase.rb', line 68 def [](id, force_load=false) if force_load then @data[id] = load(id) else @data[id] ||= load(id) end end |
#cache_all ⇒ Object
Loads all entries into cache
58 59 60 61 62 63 64 |
# File 'lib/access/yamlbase.rb', line 58 def cache_all slice = (@path.length+1)..-6 Dir.glob("#{@path}/*.yaml") { |path| # /** id = path[slice] @data[id] ||= load(id) } end |
#delete(id) ⇒ Object
Delete a record from the storage
84 85 86 87 |
# File 'lib/access/yamlbase.rb', line 84 def delete(id) @data.delete(id) File.delete(filename(id)) end |
#each ⇒ Object
Iterate over records, yielding id and object
97 98 99 100 101 102 103 |
# File 'lib/access/yamlbase.rb', line 97 def each slice = (@path.length+1)..-6 Dir.glob("#{@path}/*.yaml") { |path| id = path[slice] yield(id, @data[id] || load(id)) } end |
#each_key ⇒ Object
Iterate over records, yielding id
106 107 108 109 110 |
# File 'lib/access/yamlbase.rb', line 106 def each_key Dir.glob("#{@path}/*.yaml") { |path| yield(path2id(path)) } end |
#escape(id) ⇒ Object
Escapes path names
42 43 44 |
# File 'lib/access/yamlbase.rb', line 42 def escape(id) id.gsub(/[\x00-\x1f.%]/) { |m| "%%%02x"%m }.gsub("/", ".") end |
#exists?(id) ⇒ Boolean Also known as: exist?
Check existency of an object.
113 114 115 |
# File 'lib/access/yamlbase.rb', line 113 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
37 38 39 |
# File 'lib/access/yamlbase.rb', line 37 def filename(id) "#{@path}/#{escape(id)}.yaml" end |
#keys ⇒ Object
All record id’s
90 91 92 93 94 |
# File 'lib/access/yamlbase.rb', line 90 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
52 53 54 55 |
# File 'lib/access/yamlbase.rb', line 52 def load(id) file = filename(id) File.exist?(file) ? YAML.load_file(file) : nil end |
#path2id(path) ⇒ Object
Extracts the record-id from a path
47 48 49 |
# File 'lib/access/yamlbase.rb', line 47 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.
119 120 121 122 123 124 125 126 127 |
# File 'lib/access/yamlbase.rb', line 119 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 |