Class: ProxyMachine::Proxy

Inherits:
BasicObject
Defined in:
lib/proxy_machine/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, params = nil) ⇒ Proxy

Returns a new instance of Proxy.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/proxy_machine/proxy.rb', line 5

def initialize object, params = nil
  @object = object                    
  @before = @before_all = @after = @after_all = nil
  @allow_dinamic = false
  @avoid_original_execution = false
  
  if params
    @allow_dinamic = params[:allow_dinamic]
    @avoid_original_execution = params[:avoid_original_execution]
    @before = params[:before]
    @before_all = params[:before_all]
    @after = params[:after]
    @after_all = params[:after_all]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Raises:

  • (NoMethodError)


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

def method_missing(symbol, *args)
  @method_symbol = symbol        
  @method = @method_symbol.to_s

  raise NoMethodError.new(@method) unless @allow_dinamic or @object.methods.member?(@method)
  
  execute_call(@before_all, @object, @method_symbol, args)
  execute_call(@before, @object, args)
                          
  @result = @avoid_original_execution ? nil : @object.send(@method, *args)

  result_after = execute_call(@after, @object, @result, args)
  result_after_all = execute_call(@after_all, @object, @result, @method_symbol, args)

  return result_after_all if result_after_all
  return result_after if result_after
  @result
end

Instance Method Details

#original_objectObject



41
# File 'lib/proxy_machine/proxy.rb', line 41

def original_object; @object end

#proxied_class?Boolean

Returns:

  • (Boolean)


40
# File 'lib/proxy_machine/proxy.rb', line 40

def proxied_class?; true end