Sha256: b387c2d91c2e6448667b4f4a44ae07ff02a9018eeed4ba0c3c1d1e4a027fdcc4

Contents?: true

Size: 1.74 KB

Versions: 28

Compression:

Stored size: 1.74 KB

Contents

module Parenting
  module ClassMethods
  end
  
  module InstanceMethods
    def context_stack
      $context_stack ||= []
    end
    def initialize(o, &block)
      run_in_context do
        instance_eval(&block) if block
        super(&block)
      end
    end
    
    def run_in_context(&block)
      @parent = parent
      context_stack.push self
      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 this_context.respond_to?(m)
        #   this_context.send(m,*args,&block)
        begin
          parent.send(m,*args, &block)
        rescue Exception => e
          dputs [:parenting_exception, m, context_stack.map {|a| a.class }, current_context.map {|a| a.class }, parent.inspect] 
          super
        end
        
        # if parent.respond_to?(m)
        #   parent.send(m,*args,&block)
        # else
        #   p [:parent, parent.class, m]
        #   super
        # end
      end
    end
  end
  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
auser-poolparty-1.3.0 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.1 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.10 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.11 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.12 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.13 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.14 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.15 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.16 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.17 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.2 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.3 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.4 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.5 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.6 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.7 vendor/gems/parenting/lib/parenting/parenting.rb
auser-poolparty-1.3.8 vendor/gems/parenting/lib/parenting/parenting.rb
fairchild-poolparty-1.3.17 vendor/gems/parenting/lib/parenting/parenting.rb
fairchild-poolparty-1.3.5 vendor/gems/parenting/lib/parenting/parenting.rb
poolparty-1.3.15 vendor/gems/parenting/lib/parenting/parenting.rb