Class: Abbey::EntityStorage

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(settings) ⇒ EntityStorage

Returns a new instance of EntityStorage.

Parameters:



16
17
18
# File 'lib/entity_storage.rb', line 16

def initialize(settings)
  @settings = settings
end

Instance Attribute Details

#settingsSettings

Returns:



13
14
15
# File 'lib/entity_storage.rb', line 13

def settings
  @settings
end

Instance Method Details

#delete(namespace, key) ⇒ Object

Delete an item

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • void

Raises:



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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • void



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?

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Boolean)


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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Object)

Raises:



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.

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Hash)

    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

Parameters:

  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Boolean)


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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Set)

    List of all keys 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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • data (Object)

Returns:

  • void

Raises:



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

Returns:

  • void



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?

Returns:

  • (Boolean)

    void



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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)

Returns:

  • (Object)


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

Parameters:

  • namespace (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • key (String, Fixnum, Symbol or any other object whose #to_s method returns string)
  • data (Object)

Returns:

  • void

Raises:



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