Class: LookupStack

Inherits:
Object
  • Object
show all
Defined in:
lib/proc_tweaker.rb

Instance Method Summary collapse

Constructor Details

#initialize(bindings = []) ⇒ LookupStack

Returns a new instance of LookupStack.



2
3
4
# File 'lib/proc_tweaker.rb', line 2

def initialize(bindings = [])
  @bindings = bindings
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Raises:

  • (NoMethodError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/proc_tweaker.rb', line 6

def method_missing(m, *args)
  @bindings.reverse_each do |bind|
    begin
      method = eval('method(%s)' % m.inspect, bind)
    rescue NameError
    else
      return method.call(*args)
    end
    begin
      value = eval(m.to_s, bind)
      return value
    rescue NameError
    end
  end
  raise NoMethodError
end

Instance Method Details

#push_binding(bind) ⇒ Object



23
24
25
# File 'lib/proc_tweaker.rb', line 23

def push_binding(bind)
  @bindings.push bind
end

#push_hash(vars) ⇒ Object



31
32
33
# File 'lib/proc_tweaker.rb', line 31

def push_hash(vars)
  push_instance Struct.new(*vars.keys).new(*vars.values)
end

#push_instance(obj) ⇒ Object



27
28
29
# File 'lib/proc_tweaker.rb', line 27

def push_instance(obj)
  @bindings.push obj.instance_eval { binding }
end

#run_proc(p, *args) ⇒ Object



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

def run_proc(p, *args)
  instance_exec(*args, &p)
end