Class: Kward::PluginStore
- Inherits:
-
Object
- Object
- Kward::PluginStore
- 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
-
#plugin_id ⇒ Object
readonly
Returns the value of attribute plugin_id.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(plugin_id, root: ConfigFiles.config_dir) ⇒ PluginStore
constructor
A new instance of PluginStore.
- #put(key, value) ⇒ Object
-
#scoped(namespace) ⇒ Object
Returns a view whose keys cannot overlap another plugin-owned scope.
Constructor Details
#initialize(plugin_id, root: ConfigFiles.config_dir) ⇒ PluginStore
Returns a new instance of PluginStore.
20 21 22 23 24 25 |
# File 'lib/kward/plugins/host.rb', line 20 def initialize(plugin_id, root: ConfigFiles.config_dir) @plugin_id = validate_key(plugin_id, "plugin id") @path = File.join(File.(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.
18 19 20 |
# File 'lib/kward/plugins/host.rb', line 18 def plugin_id @plugin_id end |
Instance Method Details
#delete(key) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/kward/plugins/host.rb', line 41 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
27 28 29 30 |
# File 'lib/kward/plugins/host.rb', line 27 def get(key) key = validate_key(key, "storage key") @mutex.synchronize { copy(@values[key]) } end |
#put(key, value) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/kward/plugins/host.rb', line 32 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.
52 53 54 |
# File 'lib/kward/plugins/host.rb', line 52 def scoped(namespace) PluginScopedStore.new(self, namespace) end |