Module: Parenting::InstanceMethods

Defined in:
lib/parenting/parenting.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parenting/parenting.rb', line 38

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

Instance Method Details

#context_stackObject



6
7
8
# File 'lib/parenting/parenting.rb', line 6

def context_stack
  $context_stack ||= []
end

#current_contextObject



29
30
31
# File 'lib/parenting/parenting.rb', line 29

def current_context
  @current_context ||= context_stack[0..depth]
end

#depthObject



32
33
34
# File 'lib/parenting/parenting.rb', line 32

def depth
  @depth ||= context_stack.size
end

#headObject



18
19
20
# File 'lib/parenting/parenting.rb', line 18

def head
  context_stack.first
end

#parentObject



25
26
27
# File 'lib/parenting/parenting.rb', line 25

def parent
  @parent ||= current_context[-1] == self ? current_context[-2] : current_context[-1]
end

#run_in_context(&block) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/parenting/parenting.rb', line 9

def run_in_context(&block)
  @parent = parent

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

#thisObject



35
36
37
# File 'lib/parenting/parenting.rb', line 35

def this
  @this ||= self
end

#this_contextObject



21
22
23
24
# File 'lib/parenting/parenting.rb', line 21

def this_context
  # @this_context ||= context_stack.last
  context_stack.last
end