Class: RightSupport::Log::Multiplexer

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

Overview

A log multiplexer that uses method_missing to dispatch method calls to zero or more target loggers. Caveat emptor: you are responsible for ensuring that all of the targets respond to all of the messages you care to send; this class will perform no error checking for you!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*targets) ⇒ Multiplexer

Create a new multiplexer with a default list of targets.

Parameters

targets(Object)

Targets that should receive the method calls



39
40
41
# File 'lib/right_support/log/multiplexer.rb', line 39

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



86
87
88
89
# File 'lib/right_support/log/multiplexer.rb', line 86

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 list of multiplexed objects.



30
31
32
# File 'lib/right_support/log/multiplexer.rb', line 30

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



74
75
76
# File 'lib/right_support/log/multiplexer.rb', line 74

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



50
51
52
53
# File 'lib/right_support/log/multiplexer.rb', line 50

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



62
63
64
65
# File 'lib/right_support/log/multiplexer.rb', line 62

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

#respond_to?(m, include_all = false) ⇒ Boolean

Adhere to method_missing/respond_to metacontract: claim we respond to the message if any of our targets does.

Dispatch will still fail if not ALL of the targets respond to a given method, hence the caveat that the user of this class needs to ensure his targets are duck-type compatible with the way they’ll be used.

Parameters

m(Symbol)

Name of method

include_all(true|false)

Required for compatibility with core; ignored

Return

respond_to(true|false)

true if this object or its targets respond to m, false otherwise

Returns:

  • (Boolean)


105
106
107
# File 'lib/right_support/log/multiplexer.rb', line 105

def respond_to?(m, include_all=false)
  super(m) || @targets.any? { |t| t.respond_to?(m) }
end

#respond_to_missing?(m, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/right_support/log/multiplexer.rb', line 109

def respond_to_missing?(m, include_all=false)
  super || @targets.any? { |t| t.respond_to?(m) }
end