class Surrounded::Context::Negotiator

Public Class Methods

for_role(mod) click to toggle source

Return a class which has methods defined to forward the method to the wrapped object delegating to the behavior module. This prevents hits to method_missing.

# File lib/surrounded/context/negotiator.rb, line 8
def for_role(mod)
  klass = Class.new(self)
  # Define access to the provided module
  klass.send(:define_method, :__behaviors__) do
    mod
  end
  # For each method in the module, directly forward to the wrapped object to
  # circumvent method_missing
  mod.instance_methods(false).each do |meth|
    num = __LINE__; klass.class_eval %{
      def #{meth}(*args, &block)
        __behaviors__.instance_method(:#{meth}).bind(@object).call(*args, &block)
      end
    }, __FILE__, num
  end
  klass
end
new(object) click to toggle source
# File lib/surrounded/context/negotiator.rb, line 56
def initialize(object)
  @object = object
end

Private Instance Methods

method_missing(meth, *args, &block) click to toggle source
# File lib/surrounded/context/negotiator.rb, line 60
def method_missing(meth, *args, &block)
  @object.send(meth, *args, &block)
end
remove_context(&block)

These only differ in the message they send

Alias for: store_context
respond_to_missing?(meth, include_private=false) click to toggle source
# File lib/surrounded/context/negotiator.rb, line 64
def respond_to_missing?(meth, include_private=false)
  @object.respond_to?(meth, include_private)
end
store_context(&block) click to toggle source

Store the context in the wrapped object if it can do so

Calls superclass method Surrounded#store_context
# File lib/surrounded/context/negotiator.rb, line 45
def store_context(&block)
  if @object.respond_to?(__method__, true)
    @object.send(__method__, &block)
  else
    super
  end
  self
end
Also aliased as: remove_context