Class: ContextProvider
- Inherits:
-
Object
- Object
- ContextProvider
- Defined in:
- lib/context_provider.rb,
lib/context_provider/version.rb
Overview
A ContextProvider
allows data to be passed down the call stack in a explicit way while avoiding having to pass parameters just so it can be used by a nested function.
Defined Under Namespace
Classes: ContextAlreadyProvidedError, ContextNotProvidedError, Error
Constant Summary collapse
- VERSION =
'0.0.1'
Instance Method Summary collapse
-
#dangerously_set_provided_value(value) ⇒ Object
Sets provided value.
-
#dangerously_unset_provided_value ⇒ Object
Unsets provided value.
-
#get ⇒ Object
Gets value from provided by the context.
-
#initialize ⇒ ContextProvider
constructor
A new instance of ContextProvider.
-
#provide(value, &block) ⇒ Object
Provided value that can be accessed using
get
inside the nested block. -
#value_provided? ⇒ Boolean
Returns
true
when value has been provided (inside aprovide
block)false
otherwise. -
#where_provide_was_called ⇒ Array
Returns the callstack for where
provide
was called.
Constructor Details
#initialize ⇒ ContextProvider
Returns a new instance of ContextProvider.
24 25 26 27 |
# File 'lib/context_provider.rb', line 24 def initialize @provided = Concurrent::ThreadLocalVar.new @provide_call_stack = Concurrent::ThreadLocalVar.new end |
Instance Method Details
#dangerously_set_provided_value(value) ⇒ Object
Sets provided value. Doesn’t perform any checks. Doesn’t clean up (see dangerously_unset_provided_value
). Should only be used as a last resort.
92 93 94 |
# File 'lib/context_provider.rb', line 92 def dangerously_set_provided_value(value) set_provided_value(value) end |
#dangerously_unset_provided_value ⇒ Object
Unsets provided value. Doesn’t perform any checks. Meant to be used in conjunction with dangerously_set_provided_value
. Should only be used as a last resort.
100 101 102 |
# File 'lib/context_provider.rb', line 100 def dangerously_unset_provided_value unset_provided_value end |
#get ⇒ Object
Gets value from provided by the context.
54 55 56 57 58 59 60 61 |
# File 'lib/context_provider.rb', line 54 def get unless value_provided? raise ContextNotProvidedError, 'Unable to get context value as it has not been defined!' end @provided.value end |
#provide(value, &block) ⇒ Object
Provided value that can be accessed using get
inside the nested block.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/context_provider.rb', line 35 def provide(value, &block) if value_provided? raise ContextAlreadyProvidedError, 'Failed to provide context because it has already been provided!' end begin set_provided_value(value) block.call ensure unset_provided_value end end |
#value_provided? ⇒ Boolean
Returns true
when value has been provided (inside a provide
block) false
otherwise.
67 68 69 |
# File 'lib/context_provider.rb', line 67 def value_provided? @provide_call_stack.value != nil end |
#where_provide_was_called ⇒ Array
Returns the callstack for where provide
was called.
76 77 78 79 80 81 82 83 84 |
# File 'lib/context_provider.rb', line 76 def where_provide_was_called unless value_provided? raise ContextNotProvidedError, 'Unable to get the provide callstack because context has not been ' \ 'provided!' end @provide_call_stack.value end |