Class: UltraMarathon::SubContext

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/ultra_marathon/sub_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(context, &run_block) ⇒ self

Initializes the SubContext and defines #call as the passed in run_block

Parameters:

  • context (Object)

    the context in which to run the run_block. Any logging calls will be intercepted to allow threaded execution.

  • run_block (Proc)

    the block to be run in the given context



11
12
13
14
15
16
# File 'lib/ultra_marathon/sub_context.rb', line 11

def initialize(context, &run_block)
  @__context = context
  # Ruby cannot marshal procs or lambdas, so we need to define a method.
  # Binding to self allows us to intercept logging calls.
  define_singleton_method(:call, run_block.bind(self))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If the original context responds, including private methods, delegate to it

Parameters:

  • method (Symbol)

    the method called. If context responds to this method, it will be called on the context

  • args (Array)

    the arguments the method was called with

  • block (Proc)

    proc called with method, if applicable

Raises:

  • (NoMethodError)

    if the context, including private methods, does not respond to the method called



27
28
29
30
31
32
33
34
# File 'lib/ultra_marathon/sub_context.rb', line 27

def method_missing(method, *args, &block)

  if __context.respond_to?(method, true)
    __context.send(method, *args, &block)
  else
    raise NoMethodError.new("undefined local variable or method `#{method.to_s}' for #{__context.class.name}")
  end
end

Instance Attribute Details

#__contextObject (readonly)

Returns the value of attribute __context.



4
5
6
# File 'lib/ultra_marathon/sub_context.rb', line 4

def __context
  @__context
end