Class: RubySmart::SimpleLogger::Devices::MultiDevice
- Inherits:
-
Object
- Object
- RubySmart::SimpleLogger::Devices::MultiDevice
- Defined in:
- lib/ruby_smart/simple_logger/devices/multi_device.rb
Instance Attribute Summary collapse
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear! ⇒ Object
clears all devices.
-
#close ⇒ Object
disables writing.
-
#initialize ⇒ MultiDevice
constructor
A new instance of MultiDevice.
-
#logs ⇒ Array
returns logs from the first loggable device.
-
#register(dev, formatter = nil) ⇒ Object
registers a new device.
-
#reopen ⇒ Object
enables writing.
-
#write(data) ⇒ Object
(also: #<<)
pass data to the devices.
Constructor Details
#initialize ⇒ MultiDevice
Returns a new instance of MultiDevice.
15 16 17 18 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 15 def initialize @devices = [] @status = true end |
Instance Attribute Details
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
8 9 10 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 8 def devices @devices end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 9 def status @status end |
Class Method Details
.register(*args) ⇒ Object
11 12 13 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 11 def self.register(*args) new.register(*args) end |
Instance Method Details
#clear! ⇒ Object
clears all devices
47 48 49 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 47 def clear! @devices = [] end |
#close ⇒ Object
disables writing
37 38 39 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 37 def close @status = false end |
#logs ⇒ Array
returns logs from the first loggable device
71 72 73 74 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 71 def logs logdev = devices.detect { |device| device[:dev].respond_to?(:logs) } logdev.nil? ? [] : logdev[:dev].logs end |
#register(dev, formatter = nil) ⇒ Object
registers a new device. CHAINABLE
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 55 def register(dev, formatter = nil) # check device, to prevent nested sets of +MultiDevice+ if dev.is_a?(MultiDevice) @devices += dev.devices else @devices << { dev: dev, formatter: formatter } end self end |
#reopen ⇒ Object
enables writing
42 43 44 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 42 def reopen @status = true end |
#write(data) ⇒ Object Also known as: <<
pass data to the devices
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 22 def write(data) return false unless status devices.each do |device| if device[:formatter] device[:dev].write(device[:formatter].(*data)) else device[:dev].write(data) end end end |