Sha256: 83fbeabf2b77b0e55b2e801cf85abcc859125a4b3659a4f9128ecd26132a3ccf

Contents?: true

Size: 1020 Bytes

Versions: 4

Compression:

Stored size: 1020 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(ctxt, &block)
    accessor = block.binding.eval('self')
    if accessor.role_player?(self)
      surroundings.unshift(ctxt)
    end
    self
  end

  def remove_context(&block)
    accessor = block.binding.eval('self')
    if accessor.role_player?(self)
      surroundings.shift
    end
    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

4 entries across 4 versions & 1 rubygems

Version Path
surrounded-0.9.1 lib/surrounded.rb
surrounded-0.9.0 lib/surrounded.rb
surrounded-0.8.4 lib/surrounded.rb
surrounded-0.8.3 lib/surrounded.rb