Class: MethodInterceptor::Proxy

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

Instance Method Summary collapse

Constructor Details

#initialize(target, p_transformers = {}, r_transformers = {}) ⇒ Proxy

Returns a new instance of Proxy.



11
12
13
14
15
# File 'lib/method_interceptor.rb', line 11

def initialize(target, p_transformers={}, r_transformers={})
  @target = target
  @p_transformers = p_transformers
  @r_transformers = r_transformers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/method_interceptor.rb', line 21

def method_missing(m, *args, &block)
  params = @target.method(m).parameters.map(&:last)
  t_args = args.dup
  params.each_with_index do |param, ix|
    if @p_transformers[[m, param]] &&
      @p_transformers[[m, param]].kind_of?(Proc)
        t_args[ix] =
          @p_transformers[[m, param]].call(t_args[ix])
    end
  end
  r = @target.send(m, *t_args, &block)
  if @r_transformers[m] &&
    @r_transformers[m].kind_of?(Proc)
      @r_transformers[m].call(r)
  else
    r
  end
end

Instance Method Details

#respond_to?(symbol, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/method_interceptor.rb', line 17

def respond_to?(symbol, include_priv=false)
  @target.respond_to?(symbol, include_priv)
end