Skip to content
Kward Search API index

Class: Kward::PluginStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/plugins/host.rb

Overview

Private JSON-backed key/value storage scoped to one stable plugin ID.

Constant Summary collapse

KEY_PATTERN =
/\A[A-Za-z0-9][A-Za-z0-9:._-]*\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_id, root: ConfigFiles.config_dir) ⇒ PluginStore

Returns a new instance of PluginStore.



18
19
20
21
22
23
# File 'lib/kward/plugins/host.rb', line 18

def initialize(plugin_id, root: ConfigFiles.config_dir)
  @plugin_id = validate_key(plugin_id, "plugin id")
  @path = File.join(File.expand_path(root), "plugin_state", @plugin_id, "state.json")
  @mutex = Mutex.new
  @values = load_state
end

Instance Attribute Details

#plugin_id ⇒ Object (readonly)

Returns the value of attribute plugin_id.



16
17
18
# File 'lib/kward/plugins/host.rb', line 16

def plugin_id
  @plugin_id
end

Instance Method Details

#delete(key) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/kward/plugins/host.rb', line 39

def delete(key)
  key = validate_key(key, "storage key")
  @mutex.synchronize do
    present = @values.key?(key)
    value = @values.delete(key)
    persist if present
    copy(value)
  end
end

#get(key) ⇒ Object



25
26
27
28
# File 'lib/kward/plugins/host.rb', line 25

def get(key)
  key = validate_key(key, "storage key")
  @mutex.synchronize { copy(@values[key]) }
end

#put(key, value) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/kward/plugins/host.rb', line 30

def put(key, value)
  key = validate_key(key, "storage key")
  @mutex.synchronize do
    @values[key] = copy(value)
    persist
  end
  value
end

#scoped(namespace) ⇒ Object

Returns a view whose keys cannot overlap another plugin-owned scope.



50
51
52
# File 'lib/kward/plugins/host.rb', line 50

def scoped(namespace)
  PluginScopedStore.new(self, namespace)
end