Module: Simple::Workflow::CurrentContext

Included in:
Simple::Workflow
Defined in:
lib/simple/workflow/current_context.rb

Instance Method Summary collapse

Instance Method Details

#current_contextObject

Returns the current context.

This method never returns nil - it raises a ContextMissingError exception if the context was not initialized (via Simple::Workflow.with_context).



7
8
9
# File 'lib/simple/workflow/current_context.rb', line 7

def current_context
  Thread.current[:"Simple::Workflow.current_context"] || raise(ContextMissingError)
end

#loggerObject

Returns a logger

Returns a logger if a context is set and contains a logger.



14
15
16
17
# File 'lib/simple/workflow/current_context.rb', line 14

def logger
  current_context = Thread.current[:"Simple::Workflow.current_context"]
  current_context&.logger?
end

#with_context(ctx = nil, &block) ⇒ Object

yields a block with a given context, and restores the previous context object afterwards.



21
22
23
24
25
26
27
28
29
# File 'lib/simple/workflow/current_context.rb', line 21

def with_context(ctx = nil, &block)
  old_ctx = Thread.current[:"Simple::Workflow.current_context"]

  Thread.current[:"Simple::Workflow.current_context"] = Context.new(ctx, old_ctx)

  block.call
ensure
  Thread.current[:"Simple::Workflow.current_context"] = old_ctx
end