Class: Abbey::EntityStorage
- Inherits:
-
Object
- Object
- Abbey::EntityStorage
- Defined in:
- lib/entity_storage.rb
Overview
Represents the store. Manages the data for you. JSON serialization under the hood.
Constant Summary collapse
- ForbiddenChars =
'/'
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#delete(namespace, key) ⇒ Object
Delete an item.
-
#drop(namespace) ⇒ Object
Delete all items in the namespace.
-
#exists?(namespace, key) ⇒ Boolean
Does the item exist?.
-
#get(namespace, key) ⇒ Object
Fetch an item.
-
#get_all(namespace) ⇒ Hash
Hash of all items in the namespace, indexed by items’ keys.
-
#identifier_valid?(key) ⇒ Boolean
Check whether the key is formally valid.
-
#initialize(settings) ⇒ EntityStorage
constructor
A new instance of EntityStorage.
-
#list(namespace) ⇒ Set
Get keys of all entries in the namespace.
-
#save(namespace, key, data) ⇒ Object
Save an item.
-
#set_up! ⇒ Object
Set up the directory structure.
-
#set_up? ⇒ Boolean
Is everything ready?.
-
#try_get(namespace, key) ⇒ Object
Fetch an item or return nil.
-
#update(namespace, key, data) ⇒ Object
Update an item.
Constructor Details
#initialize(settings) ⇒ EntityStorage
Returns a new instance of EntityStorage.
16 17 18 |
# File 'lib/entity_storage.rb', line 16 def initialize(settings) @settings = settings end |
Instance Attribute Details
Instance Method Details
#delete(namespace, key) ⇒ Object
Delete an item
108 109 110 111 112 113 |
# File 'lib/entity_storage.rb', line 108 def delete(namespace, key) path = make_path(namespace, key) raise ItemNotFoundError, "Item '#{make_key(namespace,key)}' not found" unless exists?(namespace, key) File.delete(path) settings.logger.info("Deleted #{make_key(namespace,key)}") end |
#drop(namespace) ⇒ Object
Delete all items in the namespace
135 136 137 |
# File 'lib/entity_storage.rb', line 135 def drop(namespace) list(namespace).each {|item| delete(namespace, item)} end |
#exists?(namespace, key) ⇒ Boolean
Does the item exist?
75 76 77 78 79 |
# File 'lib/entity_storage.rb', line 75 def exists?(namespace, key) exists = File.exist?(make_path(namespace, key)) settings.logger.info("Queried #{make_key(namespace, key)}, found: #{exists}") exists end |
#get(namespace, key) ⇒ Object
Fetch an item
52 53 54 55 56 57 |
# File 'lib/entity_storage.rb', line 52 def get(namespace, key) path = make_path(namespace, key) raise ItemNotFoundError, "Item '#{make_key(namespace,key)}' not found" unless exists?(namespace, key) settings.logger.info("Read #{make_key(namespace, key)}") MultiJson.decode(File.read(path)) end |
#get_all(namespace) ⇒ Hash
Returns Hash of all items in the namespace, indexed by items’ keys.
126 127 128 129 130 |
# File 'lib/entity_storage.rb', line 126 def get_all(namespace) result = {} list(namespace).each {|key| result[key] = get(namespace, key)} result end |
#identifier_valid?(key) ⇒ Boolean
Check whether the key is formally valid
142 143 144 145 146 147 |
# File 'lib/entity_storage.rb', line 142 def identifier_valid?(key) key = key.to_s ForbiddenChars.each_char do |char| return false if key.include?(char) end end |
#list(namespace) ⇒ Set
Get keys of all entries in the namespace
118 119 120 121 122 |
# File 'lib/entity_storage.rb', line 118 def list(namespace) list = Dir.entries(make_path(namespace)) - %w{. ..} list.map! {|item| File.split(item)[1].to_sym} list.to_set end |
#save(namespace, key, data) ⇒ Object
Save an item
87 88 89 90 |
# File 'lib/entity_storage.rb', line 87 def save(namespace, key, data) raise ItemAlreadyPresentError, "Item '#{make_key(namespace,key)}' already exists" if exists?(namespace, key) unsafe_save(namespace, key, data) end |
#set_up! ⇒ Object
Set up the directory structure
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/entity_storage.rb', line 36 def set_up! return if set_up? settings.namespaces.each do |ns| path = make_path(ns) if !Dir.exists?(path) then settings.logger.debug("Creating dir #{path}") FileUtils.mkdir_p(path) end end end |
#set_up? ⇒ Boolean
Is everything ready?
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/entity_storage.rb', line 22 def set_up? ready = true dirs = [settings.path] settings.namespaces.each {|x| dirs << make_path(x)} dirs.each do |dir| existent, writable = Dir.exists?(dir), File.writable?(dir) settings.logger.debug("#{dir} -- exists: #{existent}, writable: #{writable}") ready &&= existent && writable end ready end |
#try_get(namespace, key) ⇒ Object
Fetch an item or return nil
63 64 65 66 67 68 69 |
# File 'lib/entity_storage.rb', line 63 def try_get(namespace, key) begin return get(namespace, key) rescue ItemNotFoundError return nil end end |
#update(namespace, key, data) ⇒ Object
Update an item. Shorthand for delete & save
98 99 100 101 |
# File 'lib/entity_storage.rb', line 98 def update(namespace, key, data) raise ItemNotFoundError, "Item '#{make_key(namespace,key)}' not found" unless exists?(namespace, key) unsafe_save(namespace, key, data) end |