Module: Philiprehberger::StateBag
- Defined in:
- lib/philiprehberger/state_bag.rb,
lib/philiprehberger/state_bag/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.3.0'
Class Method Summary collapse
-
.clear ⇒ void
Clear all entries from the thread-local state bag.
-
.delete(key) ⇒ Object
Remove a key from the state bag.
-
.empty? ⇒ Boolean
Check whether the state bag is empty.
-
.fetch(key, default = UNSET) {|key| ... } ⇒ Object
Fetch a value from the state bag with strict key checking.
-
.get(key, default = nil) ⇒ Object
Get a value from the thread-local state bag.
-
.key?(key) ⇒ Boolean
Check if a key exists in the state bag.
-
.keys ⇒ Array
Return all keys in the state bag.
-
.set(key, val) ⇒ Object
Set a value in the thread-local state bag.
-
.size ⇒ Integer
Return the number of entries in the state bag.
-
.to_h ⇒ Hash
Return a snapshot of the current state as a hash.
-
.with(**overrides) { ... } ⇒ Object
Execute a block with temporary state, restoring previous values after.
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clear all entries from the thread-local state bag
57 58 59 |
# File 'lib/philiprehberger/state_bag.rb', line 57 def self.clear store.clear end |
.delete(key) ⇒ Object
Remove a key from the state bag
91 92 93 |
# File 'lib/philiprehberger/state_bag.rb', line 91 def self.delete(key) store.delete(key) end |
.empty? ⇒ Boolean
Check whether the state bag is empty.
113 114 115 |
# File 'lib/philiprehberger/state_bag.rb', line 113 def self.empty? store.empty? end |
.fetch(key, default = UNSET) {|key| ... } ⇒ Object
Fetch a value from the state bag with strict key checking
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/philiprehberger/state_bag.rb', line 75 def self.fetch(key, default = UNSET, &block) if store.key?(key) store[key] elsif block block.call(key) elsif default != UNSET default else raise KeyError, "key not found: #{key.inspect}" end end |
.get(key, default = nil) ⇒ Object
Get a value from the thread-local state bag
26 27 28 |
# File 'lib/philiprehberger/state_bag.rb', line 26 def self.get(key, default = nil) store.fetch(key, default) end |
.key?(key) ⇒ Boolean
Check if a key exists in the state bag
99 100 101 |
# File 'lib/philiprehberger/state_bag.rb', line 99 def self.key?(key) store.key?(key) end |
.keys ⇒ Array
Return all keys in the state bag.
120 121 122 |
# File 'lib/philiprehberger/state_bag.rb', line 120 def self.keys store.keys end |
.set(key, val) ⇒ Object
Set a value in the thread-local state bag
17 18 19 |
# File 'lib/philiprehberger/state_bag.rb', line 17 def self.set(key, val) store[key] = val end |
.size ⇒ Integer
Return the number of entries in the state bag.
106 107 108 |
# File 'lib/philiprehberger/state_bag.rb', line 106 def self.size store.size end |
.to_h ⇒ Hash
Return a snapshot of the current state as a hash
64 65 66 |
# File 'lib/philiprehberger/state_bag.rb', line 64 def self.to_h store.dup end |
.with(**overrides) { ... } ⇒ Object
Execute a block with temporary state, restoring previous values after
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/philiprehberger/state_bag.rb', line 35 def self.with(**overrides, &block) previous = {} missing = [] overrides.each do |k, v| if store.key?(k) previous[k] = store[k] else missing << k end store[k] = v end block.call ensure previous.each { |k, v| store[k] = v } missing.each { |k| store.delete(k) } end |