Class: ContextProvider

Inherits:
Object
  • Object
show all
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.

Examples:

context = ContextProvider.new

context.provide("bar") do
  context.get #=> "bar"
end

Defined Under Namespace

Classes: ContextAlreadyProvidedError, ContextNotProvidedError, Error

Constant Summary collapse

VERSION =
'0.0.1'

Instance Method Summary collapse

Constructor Details

#initializeContextProvider

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.

Parameters:

  • value (Object)


92
93
94
# File 'lib/context_provider.rb', line 92

def dangerously_set_provided_value(value)
  set_provided_value(value)
end

#dangerously_unset_provided_valueObject

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

#getObject

Gets value from provided by the context.

Returns:

  • (Object)

    provided value



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.

Parameters:

  • value (Object)

    Value to be passed down the stack

  • block (Proc)

    Block where provided value is going to be available.

Raises:

  • (ContextAlreadyDefinedError)

    if context has already been provide



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.

Returns:

  • (Boolean)


67
68
69
# File 'lib/context_provider.rb', line 67

def value_provided?
  @provide_call_stack.value != nil
end

#where_provide_was_calledArray

Returns the callstack for where provide was called.

Returns:

  • (Array)

    callstack for where provide was called

Raises:



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