Class: AetherObservatory::ObserverBase

Inherits:
Object
  • Object
show all
Defined in:
lib/aether_observatory/observer_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ ObserverBase

Returns a new instance of ObserverBase.



99
100
101
# File 'lib/aether_observatory/observer_base.rb', line 99

def initialize(event)
  self.event = event
end

Instance Attribute Details

#eventObject

Returns the value of attribute event.



97
98
99
# File 'lib/aether_observatory/observer_base.rb', line 97

def event
  @event
end

Class Method Details

.inherited(subclass) ⇒ Object



6
7
8
9
10
11
# File 'lib/aether_observatory/observer_base.rb', line 6

def inherited(subclass)
  super
  subclass.instance_variable_set(:@subscribed_topics, Set.new)
  subclass.instance_variable_set(:@state, :stopped)
  subclass.instance_variable_set(:@subscriptions, {})
end

.startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aether_observatory/observer_base.rb', line 13

def start
  return if started?

  logger.debug("[#{name}] Starting")

  subscribed_to.each do |topic|
    next if subscriptions.include?(topic)

    register_subscription_to(topic)
  end

  self.state = :started
end

.started?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/aether_observatory/observer_base.rb', line 59

def started?
  state == :started
end

.stopObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aether_observatory/observer_base.rb', line 27

def stop
  return if stopped?

  logger.debug("[#{name}] Stopping")

  subscriptions.each_key do |topic|
    unregister_subscription_to(topic)
  end

  self.state = :stopped
end

.stopped?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/aether_observatory/observer_base.rb', line 63

def stopped?
  state == :stopped
end

.subscribe_to(topic) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/aether_observatory/observer_base.rb', line 39

def subscribe_to(topic)
  subscribed_topics.add(topic)

  return if stopped?

  register_subscription_to(topic)
end

.subscribed_toObject



55
56
57
# File 'lib/aether_observatory/observer_base.rb', line 55

def subscribed_to
  subscribed_topics.to_a
end

.unsubscribe_from(topic) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/aether_observatory/observer_base.rb', line 47

def unsubscribe_from(topic)
  subscribed_topics.delete(topic)

  return if stopped?

  unregister_subscription_to(topic)
end