Class: RightScale::Multiplexer

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

Overview

Apply each method call to all registered targets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*targets) ⇒ Multiplexer

Initialize multiplexer targets

Parameters

targets(Object)

Targets that should receive the method calls



38
39
40
# File 'lib/right_agent/multiplexer.rb', line 38

def initialize(*targets)
  @targets = targets || []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Forward any method invocation to targets

Parameters

m(Symbol)

Method that should be multiplexed

args(Array)

Arguments

Return

res(Object)

Result of first target in list



85
86
87
88
# File 'lib/right_agent/multiplexer.rb', line 85

def method_missing(m, *args)
  res = @targets.inject([]) { |res, t| res << t.__send__(m, *args) }
  res[0]
end

Instance Attribute Details

#targetsObject (readonly)

Access to underlying multiplexed objects



29
30
31
# File 'lib/right_agent/multiplexer.rb', line 29

def targets
  @targets
end

Instance Method Details

#[](index) ⇒ Object

Access target at given index

Parameters

index(Integer)

Target index

Return

target(Object)

Target at index ‘index’ or nil if none



73
74
75
# File 'lib/right_agent/multiplexer.rb', line 73

def [](index)
  target = @targets[index]
end

#add(target) ⇒ Object

Add object to list of multiplexed targets

Parameters

target(Object)

Add target to list of multiplexed targets

Return

self(RightScale::Multiplexer)

self so operation can be chained



49
50
51
52
# File 'lib/right_agent/multiplexer.rb', line 49

def add(target)
  @targets << target unless @targets.include?(target)
  self
end

#remove(target) ⇒ Object

Remove object from list of multiplexed targets

Parameters

target(Object)

Remove target from list of multiplexed targets

Return

self(RightScale::Multiplexer)

self so operation can be chained



61
62
63
64
# File 'lib/right_agent/multiplexer.rb', line 61

def remove(target)
  @targets.delete_if { |t| t == target }
  self
end

#respond_to?(m) ⇒ Boolean

Determine whether this object, or ALL of its targets, responds to the named method.

Parameters

m(Symbol)

Forwarded method name

Return

(true|false)

True if this object, or ALL targets, respond to the names method; false otherwise

Returns:

  • (Boolean)


98
99
100
# File 'lib/right_agent/multiplexer.rb', line 98

def respond_to?(m)
  super(m) || @targets.all? { |t| t.respond_to?(m) }
end