module DCI::Context::DSL

Private Instance Methods

define_entry_using_method(name) click to toggle source

Replace an existing method with a wrapper that advertises the current context

# File lib/dci/context.rb, line 32
def define_entry_using_method(name)
  method = instance_method(name)
  define_method(name) do |*arguments, &block|
    begin
      # Swap out the currently executing context
      Thread.current[:'DCI::Context.current'], old_context = self, Thread.current[:'DCI::Context.current']
      method.bind(self).call(*arguments, &block)
    ensure
      # Reinstate the previously executing context
      Thread.current[:'DCI::Context.current'] = old_context
    end
  end
end
define_entry_using_proc(name, definition) click to toggle source

Create a method that executes the provided definition while advertising the current context

# File lib/dci/context.rb, line 47
def define_entry_using_proc(name, definition)
  define_method(name, &definition)
  define_entry_using_method(name)
end
entry(name, proc = nil, &block) click to toggle source

Define a context entry point

# File lib/dci/context.rb, line 53
def entry(name, proc = nil, &block)
  if block_given?
    define_entry_using_proc(name, block)
  elsif proc.respond_to?(:to_proc)
    define_entry_using_proc(name, proc)
  elsif method_defined?(name)
    define_entry_using_method(name)
  else
    @entries ||= []
    @entries |= [name]
  end
end
method_added(name) click to toggle source

Listen for new methods that are intended to be entry points

# File lib/dci/context.rb, line 67
def method_added(name)
  if @entries && @entries.delete(name)
    define_entry_using_method(name)
  end
end
role(name, *args, &block) click to toggle source

Define a context role

# File lib/dci/context.rb, line 74
def role(name, *args, &block)
  attr_reader name
  public name

  if block_given?
    role_module = Module.new
    role_module.module_eval(&block)

    define_method("#{name}=") do |actor|
      instance_variable_set("@#{name}", cast(actor, :as => role_module))
    end
  else
    attr_writer name
  end

  protected "#{name}="
end
trigger(name, delegate, method = name) click to toggle source
# File lib/dci/context.rb, line 92
def trigger(name, delegate, method = name)
  entry(name) do |*arguments, &block|
    __send__(delegate).__send__(method, *arguments, &block)
  end
end