Sha256: ee2efbd857cbd4e7d83340e42a5cb1501572c19209c2968ecfa8124b624bd9e0

Contents?: true

Size: 1010 Bytes

Versions: 9

Compression:

Stored size: 1010 Bytes

Contents

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

# 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.instance
  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
    include Singleton
    def role?(*args)
      nil
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
surrounded-1.1.0 lib/surrounded.rb
surrounded-1.0.0 lib/surrounded.rb
surrounded-0.9.11 lib/surrounded.rb
surrounded-0.9.10 lib/surrounded.rb
surrounded-0.9.9 lib/surrounded.rb
surrounded-0.9.8 lib/surrounded.rb
surrounded-0.9.7 lib/surrounded.rb
surrounded-0.9.6 lib/surrounded.rb
surrounded-0.9.5 lib/surrounded.rb