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.6.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.
-
.dig(*keys) ⇒ Object?
Dig into nested hash-valued entries using a sequence of keys.
-
.each {|key, value| ... } ⇒ Enumerator, Hash
Iterate over key-value pairs in 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.
-
.merge(**entries) ⇒ Hash
Bulk-set multiple entries in the state bag.
-
.replace(hash) ⇒ Hash
Replace the entire state bag with the given hash.
-
.set(key, val) ⇒ Object
Set a value in the thread-local state bag.
-
.size ⇒ Integer
Return the number of entries in the state bag.
-
.slice(*keys) ⇒ Hash
Return a subset of the state bag containing only the given keys.
-
.to_h ⇒ Hash
Return a snapshot of the current state as a hash.
-
.values ⇒ Array
Return all values in the state bag.
-
.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
108 109 110 |
# File 'lib/philiprehberger/state_bag.rb', line 108 def self.delete(key) store.delete(key) end |
.dig(*keys) ⇒ Object?
Dig into nested hash-valued entries using a sequence of keys
Mirrors Ruby’s Hash#dig. Looks up the first key in the thread-local store, then calls :dig on the resulting value with the remaining keys. Returns nil on any missing key. Raises ArgumentError when no keys are given, and TypeError when an intermediate value does not respond to :dig (matching Hash#dig behavior).
98 99 100 101 102 |
# File 'lib/philiprehberger/state_bag.rb', line 98 def self.dig(*keys) raise ArgumentError, 'wrong number of arguments (given 0, expected 1+)' if keys.empty? store.dig(*keys) end |
.each {|key, value| ... } ⇒ Enumerator, Hash
Iterate over key-value pairs in the state bag
179 180 181 182 183 |
# File 'lib/philiprehberger/state_bag.rb', line 179 def self.each(&block) return store.dup.each_pair unless block store.dup.each_pair(&block) end |
.empty? ⇒ Boolean
Check whether the state bag is empty.
130 131 132 |
# File 'lib/philiprehberger/state_bag.rb', line 130 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
116 117 118 |
# File 'lib/philiprehberger/state_bag.rb', line 116 def self.key?(key) store.key?(key) end |
.keys ⇒ Array
Return all keys in the state bag.
137 138 139 |
# File 'lib/philiprehberger/state_bag.rb', line 137 def self.keys store.keys end |
.merge(**entries) ⇒ Hash
Bulk-set multiple entries in the state bag
152 153 154 155 |
# File 'lib/philiprehberger/state_bag.rb', line 152 def self.merge(**entries) entries.each { |k, v| store[k] = v } store.dup end |
.replace(hash) ⇒ Hash
Replace the entire state bag with the given hash
161 162 163 164 |
# File 'lib/philiprehberger/state_bag.rb', line 161 def self.replace(hash) store.replace(hash.dup) store.dup 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.
123 124 125 |
# File 'lib/philiprehberger/state_bag.rb', line 123 def self.size store.size end |
.slice(*keys) ⇒ Hash
Return a subset of the state bag containing only the given keys
170 171 172 |
# File 'lib/philiprehberger/state_bag.rb', line 170 def self.slice(*keys) store.slice(*keys) 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 |
.values ⇒ Array
Return all values in the state bag.
144 145 146 |
# File 'lib/philiprehberger/state_bag.rb', line 144 def self.values store.values 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 |