Sha256: 9088e36013d3cf47236920e74df2ced22da602da6d6bd5c6a5e9b4780f0de96c

Contents?: true

Size: 963 Bytes

Versions: 3

Compression:

Stored size: 963 Bytes

Contents

require "surrounded/version"
require "surrounded/context"

# This module should be added to objects which will enter
# into context objects. 
#
# Its main purpose is to keep a reference to the context
# and to implement method_missing to handle the relationship
# to other objects in the context.
module Surrounded

  private

  def store_context(&block)
    accessor = block.binding.eval('self')
    surroundings.unshift(accessor)
    self
  end

  def remove_context(&block)
    accessor = block.binding.eval('self')
    surroundings.shift if surroundings.include?(accessor)
    self
  end

  def surroundings
    @__surroundings__ ||= []
  end

  def context
    surroundings.first || NullContext.new
  end

  def method_missing(meth, *args, &block)
    context.role?(meth){} || super
  end

  def respond_to_missing?(meth, include_private=false)
    !!context.role?(meth){} || super
  end

  class NullContext
    def role?(*args)
      nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surrounded-0.9.4 lib/surrounded.rb
surrounded-0.9.3 lib/surrounded.rb
surrounded-0.9.2 lib/surrounded.rb