Class: Volatile::Storage
- Inherits:
-
Object
- Object
- Volatile::Storage
- Defined in:
- lib/volatile_wtf/storage.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Gets value by key (with a namespace) Returns a ‘Volatile::Pair` instance, so that #modified and #created are easily accessible.
-
#[]=(key, value) ⇒ Object
Sets value for a given key (with a namespace).
- #created(key, external: false) ⇒ Object
-
#get(key) ⇒ Object
Gets value by key (without a namespace) Returns a ‘Volatile::Pair` instance, so that #modified and #created are easily accessible.
-
#initialize(namespace = nil) ⇒ Storage
constructor
A new instance of Storage.
- #modified(key, external: false) ⇒ Object
-
#namespaced_key(key) ⇒ Object
Generates a namespaced option for a key name.
-
#reload(key) ⇒ Object
Refetch cached key.
-
#set(key, value) ⇒ Object
Sets value for a given key (without a namespace).
- #to_h(use_namespace = false) ⇒ Object
Constructor Details
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
6 7 8 |
# File 'lib/volatile_wtf/storage.rb', line 6 def namespace @namespace end |
Instance Method Details
#[](key) ⇒ Object
Gets value by key (with a namespace) Returns a ‘Volatile::Pair` instance, so that #modified and #created are easily accessible.
17 18 19 20 |
# File 'lib/volatile_wtf/storage.rb', line 17 def [](key) key = namespaced_key(key) get(key) end |
#[]=(key, value) ⇒ Object
Sets value for a given key (with a namespace). Keeps the key for later to use in ‘to_h`
24 25 26 27 28 |
# File 'lib/volatile_wtf/storage.rb', line 24 def []=(key, value) key = namespaced_key(key) @keys << key set(key, value) end |
#created(key, external: false) ⇒ Object
48 49 50 51 |
# File 'lib/volatile_wtf/storage.rb', line 48 def created(key, external: false) key = external ? key : namespaced_key(key) @manager.created(key) end |
#get(key) ⇒ Object
Gets value by key (without a namespace) Returns a ‘Volatile::Pair` instance, so that #modified and #created are easily accessible.
33 34 35 |
# File 'lib/volatile_wtf/storage.rb', line 33 def get(key) @manager.retrieve(key) end |
#modified(key, external: false) ⇒ Object
53 54 55 56 |
# File 'lib/volatile_wtf/storage.rb', line 53 def modified(key, external: false) key = external ? key : namespaced_key(key) @manager.modified(key) end |
#namespaced_key(key) ⇒ Object
Generates a namespaced option for a key name
59 60 61 |
# File 'lib/volatile_wtf/storage.rb', line 59 def namespaced_key(key) "#{@namespace}_#{key}" end |
#reload(key) ⇒ Object
Refetch cached key
44 45 46 |
# File 'lib/volatile_wtf/storage.rb', line 44 def reload(key) @manager.reload(key) end |
#set(key, value) ⇒ Object
Sets value for a given key (without a namespace)
38 39 40 41 |
# File 'lib/volatile_wtf/storage.rb', line 38 def set(key, value) @manager.store(key, value) { key => value } end |
#to_h(use_namespace = false) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/volatile_wtf/storage.rb', line 63 def to_h(use_namespace = false) manager = @manager @keys.each_with_object({}) do |key, hash| hash_key = use_namespace ? key : key.sub("#{@namespace}_", '') hash[hash_key] = manager.retrieve(key) end end |