Class: RubySmart::SimpleLogger::Devices::MultiDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_smart/simple_logger/devices/multi_device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultiDevice

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

#devicesObject (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

#statusObject (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

#closeObject

disables writing



37
38
39
# File 'lib/ruby_smart/simple_logger/devices/multi_device.rb', line 37

def close
  @status = false
end

#logsArray

returns logs from the first loggable device

Returns:

  • (Array)

    logs



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

Parameters:

  • dev (Object)


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

#reopenObject

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

Parameters:

  • data (Object)


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