Class: Stree::Roxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/stree/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.



37
38
39
40
41
42
43
44
45
# File 'lib/stree/roxy/proxy.rb', line 37

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



67
68
69
# File 'lib/stree/roxy/proxy.rb', line 67

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

Instance Method Details

#proxy_ownerObject



47
48
49
# File 'lib/stree/roxy/proxy.rb', line 47

def proxy_owner
  @owner
end

#proxy_targetObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/stree/roxy/proxy.rb', line 51

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