Class: RightSupport::Log::Multiplexer
- 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
-
#targets ⇒ Object
readonly
Access to underlying list of multiplexed objects.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access target at given index.
-
#add(target) ⇒ Object
Add object to list of multiplexed targets.
-
#initialize(*targets) ⇒ Multiplexer
constructor
Create a new multiplexer with a default list of targets.
-
#method_missing(m, *args) ⇒ Object
Forward any method invocation to targets.
-
#remove(target) ⇒ Object
Remove object from list of multiplexed targets.
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
40 41 42 |
# File 'lib/right_support/log/multiplexer.rb', line 40 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
87 88 89 90 |
# File 'lib/right_support/log/multiplexer.rb', line 87 def method_missing(m, *args) res = @targets.inject([]) { |res, t| res << t.__send__(m, *args) } res[0] end |
Instance Attribute Details
#targets ⇒ Object (readonly)
Access to underlying list of multiplexed objects
31 32 33 |
# File 'lib/right_support/log/multiplexer.rb', line 31 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
75 76 77 |
# File 'lib/right_support/log/multiplexer.rb', line 75 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
51 52 53 54 |
# File 'lib/right_support/log/multiplexer.rb', line 51 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
63 64 65 66 |
# File 'lib/right_support/log/multiplexer.rb', line 63 def remove(target) @targets.delete_if { |t| t == target } self end |