Sha256: c103bae514e048bf1dd028c30c58b6ba3469b01bae7530ccef394ea9258a8240

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

module Parenting
  module ClassMethods
  end
  
  module InstanceMethods
    def context_stack
      $context_stack ||= []
    end
    def run_in_context(&block)
      @parent = parent

      context_stack.push self
      this_context.instance_eval(&block) if block
      context_stack.pop
      head   
    end

    def head
      context_stack.first
    end
    def this_context
      # @this_context ||= context_stack.last
      context_stack.last
    end
    def parent
      @parent ||= current_context[-1] == self ? current_context[-2] : current_context[-1]
    end

    def current_context
      @current_context ||= context_stack[0..depth]
    end
    def depth
      @depth ||= context_stack.size
    end
    def this
      @this ||= self
    end
    def method_missing(m,*args,&block)
      if block
        if args.empty?
          super
        else          
          inst = args[0]
          context_stack.push self
          inst.instance_eval(&block)
          context_stack.pop
        end
      else
        if respond_to?(:parent) && parent != self && parent.respond_to?(m)
          parent.send(m,*args,&block)
        else
          super
        end
      end
    end
  end
  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
auser-parenting-0.0.2 lib/parenting/base.rb
auser-parenting-0.0.4 lib/parenting/parenting.rb
auser-poolparty-1.1.7 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.2.0 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.2.1 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.2.2 vendor/gems/parenting/lib/parenting/parenting.rb
poolparty-1.2.2 vendor/gems/parenting/lib/parenting/parenting.rb