Sha256: 95f2dbac73671586f134d55ac83be8b23b3e213cb905d7278cfead38853870dd

Contents?: true

Size: 1002 Bytes

Versions: 3

Compression:

Stored size: 1002 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
  end

  def remove_context(&block)
    accessor = block.binding.eval('self')
    if accessor.role_player?(self)
      surroundings.shift
    end
  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.8.2 lib/surrounded.rb
surrounded-0.8.1 lib/surrounded.rb
surrounded-0.8.0 lib/surrounded.rb