Sha256: 7cdb60a46909a9bb48304530a87ee9cf695a4d18625c2841508f6d9768852bf3

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

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

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

8 entries across 8 versions & 1 rubygems

Version Path
surrounded-0.7.3 lib/surrounded.rb
surrounded-0.7.2 lib/surrounded.rb
surrounded-0.7.1 lib/surrounded.rb
surrounded-0.7.0 lib/surrounded.rb
surrounded-0.6.0 lib/surrounded.rb
surrounded-0.5.1 lib/surrounded.rb
surrounded-0.5.0 lib/surrounded.rb
surrounded-0.4.1 lib/surrounded.rb