Class: Roxy::Proxy

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

Overview

The very simple proxy class that provides a basic pass-through mechanism between the proxy owner and the proxy target.

Instance Method Summary collapse

Constructor Details

#initialize(owner, options, args, &block) ⇒ Proxy

Returns a new instance of Proxy.



15
16
17
18
19
20
21
22
23
# File 'lib/roxy/proxy.rb', line 15

def initialize(owner, options, args, &block)
  @owner = owner
  @target = options[:to]
  @args = args
  
  # Adorn with user-provided proxy methods
  [options[:extend]].flatten.each { |ext| proxy_extend(ext) } if options[:extend]
  proxy_instance_eval &block if block_given?      
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegate all method calls we don’t know about to target object



38
39
40
# File 'lib/roxy/proxy.rb', line 38

def method_missing(sym, *args, &block)
  proxy_target.__send__(sym, *args, &block)
end

Instance Method Details

#proxy_ownerObject



25
# File 'lib/roxy/proxy.rb', line 25

def proxy_owner; @owner; end

#proxy_targetObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/roxy/proxy.rb', line 26

def proxy_target
  if @target.is_a?(Proc)
    @target.call(@owner)
  elsif @target.is_a?(UnboundMethod)
    bound_method = @target.bind(proxy_owner)
    bound_method.arity == 0 ? bound_method.call : bound_method.call(*@args)
  else
    @target
  end
end