module Casting::Context

Public Class Methods

extended(base) click to toggle source
# File lib/casting/context.rb, line 32
def self.extended(base)
  base.send(:include, InstanceMethods)
end
new(*setup_args, &block) click to toggle source
# File lib/casting/context.rb, line 36
    def initialize(*setup_args, &block)
      attr_reader(*setup_args)
      private(*setup_args)

      define_method(:__custom_initialize, &(block || proc {}))

      mod = Module.new
      mod.class_eval <<~INIT, __FILE__, __LINE__ + 1
        def initialize(#{setup_args.map { |name| "#{name}:" }.join(",")})
          @assignments = []
          #{setup_args.map do |name|
            ["assign(", name, ", '", name, "')"].join
          end.join("\n")}
          __custom_initialize
          Thread.current[:context] = self
        end
        attr_reader :assignments
      INIT
      const_set(:Initializer, mod)
      include mod
    end

Public Instance Methods

context() click to toggle source
# File lib/casting/context.rb, line 109
def context
  Thread.current[:context]
end
context=(obj) click to toggle source
# File lib/casting/context.rb, line 113
def context=(obj)
  Thread.current[:context] = obj
end
role(role_name) click to toggle source

Get the object playing a particular role

# File lib/casting/context.rb, line 118
def role(role_name)
  context.send(role_name)
end
tell(role_name, method_name, ...) click to toggle source

Execute the named method on the object plaing the name role

# File lib/casting/context.rb, line 123
def tell(role_name, method_name, ...)
  if context == self || context.contains?(self)
    context.dispatch(role(role_name), method_name, ...)
  end
end