Class: ShortBus::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/short_bus/monitor.rb

Overview

For printing out all messages

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Monitor



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/short_bus/monitor.rb', line 8

def initialize(*args)
  @options = {
    message_spec: nil,
    name: 'ShortBus::Monitor',
    service: method(:monitor),
    suppress_payload: false,
    suppress_publisher: false,
    publisher_spec: nil,
    thread_count: 1
  }
  @suppress_payload, @suppress_publisher = nil

  if args[0].is_a?(Hash) && args[0].key?(:driver)
    @options.merge! args[0]
    @driver = @options[:driver]
    @options.delete(:driver)
  elsif args.is_a?(Array) && args.length == 1
    @driver = args[0]
  else
    raise ArgumentError, 'No driver passed.'
  end

  @suppress_payload = @options.delete(:suppress_payload)
  @suppress_publisher = @options.delete(:suppress_publisher)

  start
end

Instance Method Details

#monitor(message) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/short_bus/monitor.rb', line 36

def monitor(message)
  puts "[#{@options[:name]}]  message = #{message}"
  printf(
    "  %s  payload = %s\n",
    @options[:name] ? ' ' * @options[:name].length : '',
    message.payload.inspect
  ) if message.payload && !@suppress_payload
  printf(
    "  %spublisher = %s\n",
    @options[:name] ? ' ' * @options[:name].length : '',
    message.publisher ? message.publisher : '*ANONYMOUS*'
  ) unless @suppress_publisher
  nil
end

#startObject



51
52
53
# File 'lib/short_bus/monitor.rb', line 51

def start
  @service = @driver.subscribe(@options)
end

#stopObject



55
56
57
# File 'lib/short_bus/monitor.rb', line 55

def stop
  @driver.unsubscribe @service
end