Sha256: 0197044bdad346a84ecd4c9fd6b8c968c76a67e664d772a8b46bd371a7fac583

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 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 parent && parent != self
          begin
            parent.send(m,*args,&block)
          rescue NoMethodError => e
            super   
          end
        else
          super
        end
      end
    end
  end
  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
auser-poolparty-1.1.1 vendor/gems/parenting/lib/parenting/base.rb
auser-poolparty-1.1.3 vendor/gems/parenting/lib/parenting/base.rb
auser-poolparty-1.1.4 vendor/gems/parenting/lib/parenting/base.rb
auser-poolparty-1.1.5 vendor/gems/parenting/lib/parenting/base.rb
fairchild-poolparty-1.1.3 vendor/gems/parenting/lib/parenting/base.rb
fairchild-poolparty-1.1.4 vendor/gems/parenting/lib/parenting/base.rb