Sha256: 7bc2c17b656a46eb2efdca7bd245b46938e9e733a4f5824217b04bcab6220154

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

require "surrounded/version"

module Surrounded
  def self.included(klass)
    klass.class_eval {
      extend Surrounded::Contextual
    }
    unless klass.is_a?(Class)
      def klass.extended(object)
        Surrounded.create_surroundings(object)
      end
    end
  end

  def self.extended(object)
    Surrounded.create_surroundings(object)
  end

  module Contextual
    def new(*args)
      instance = super
      Surrounded.create_surroundings(instance)
      instance
    end
  end

  def store_context(ctxt)
    surroundings.unshift(ctxt)
  end

  def remove_context
    surroundings.shift
  end

  private

  def self.create_surroundings(obj)
    obj.instance_variable_set(:@__surroundings__, [])
  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.4.0 lib/surrounded.rb
surrounded-0.3.1 lib/surrounded.rb
surrounded-0.3.0 lib/surrounded.rb
surrounded-0.2.1 lib/surrounded.rb