Class: Plumbing::RubberDuck::Proxy

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

Overview

Proxy object that forwards the duck-typed methods to the target object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, duck_type) ⇒ Proxy

Returns a new instance of Proxy.



7
8
9
10
# File 'lib/plumbing/rubber_duck/proxy.rb', line 7

def initialize target, duck_type
  @target = target
  @duck_type = duck_type
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/plumbing/rubber_duck/proxy.rb', line 5

def target
  @target
end

Instance Method Details

#as(duck_type) ⇒ Plumbing::RubberDuck::Proxy

Convert the proxy to the given duck-type, ensuring that existing proxies are not duplicated

Returns:



14
15
16
# File 'lib/plumbing/rubber_duck/proxy.rb', line 14

def as duck_type
  (duck_type == @duck_type) ? self : duck_type.proxy_for(target)
end