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

Class Method Details

.clearvoid

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

Parameters:

  • key (Symbol, String)

    the key to remove

Returns:

  • (Object)

    the removed value or nil



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.

Returns:

  • (Boolean)


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

Parameters:

  • key (Symbol, String)

    the key to retrieve

  • default (Object) (defaults to: UNSET)

    optional default if key is not found

Yields:

  • (key)

    optional block called when key is not found

Returns:

  • (Object)

    the stored value, default, or block result

Raises:

  • (KeyError)

    if key not found and no default or block given



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

Parameters:

  • key (Symbol, String)

    the key to retrieve

  • default (Object) (defaults to: nil)

    optional default if key is not found

Returns:

  • (Object)

    the stored value or default



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

Parameters:

  • key (Symbol, String)

    the key to check

Returns:

  • (Boolean)

    true if the key exists



99
100
101
# File 'lib/philiprehberger/state_bag.rb', line 99

def self.key?(key)
  store.key?(key)
end

.keysArray

Return all keys in the state bag.

Returns:

  • (Array)


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

Parameters:

  • key (Symbol, String)

    the key to set

  • val (Object)

    the value to store

Returns:

  • (Object)

    the stored value



17
18
19
# File 'lib/philiprehberger/state_bag.rb', line 17

def self.set(key, val)
  store[key] = val
end

.sizeInteger

Return the number of entries in the state bag.

Returns:

  • (Integer)


106
107
108
# File 'lib/philiprehberger/state_bag.rb', line 106

def self.size
  store.size
end

.to_hHash

Return a snapshot of the current state as a hash

Returns:

  • (Hash)

    copy of the current state



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

Parameters:

  • overrides (Hash)

    key-value pairs to set during the block

Yields:

  • the block to execute with the temporary state

Returns:

  • (Object)

    the return value of the block



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