Class: At::MethodToInstanceVariableProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ MethodToInstanceVariableProxy

Returns a new instance of MethodToInstanceVariableProxy.



10
11
12
# File 'lib/at.rb', line 10

def initialize(parent)
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/at.rb', line 14

def method_missing(meth, *args, &blk)
  @parent.instance_eval do
    if match = meth.to_s.match(/(.*)\=$/)
      value = args.length == 1 ? args.first : args
      instance_variable_set("@#{match[1]}", value)
    else
      if block_given?
        instance_variable_set("@#{meth}", args.empty? ? args : blk.call)
      else
        if args.empty?
          instance_variable_get("@#{meth}")
        else
            value = args.length == 1 ? args.first : args
          instance_variable_set("@#{meth}", value)
        end
      end
    end
  end
end