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



8
9
10
11
12
13
# File 'lib/aether_observatory/observer_base.rb', line 8

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



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

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)


61
62
63
# File 'lib/aether_observatory/observer_base.rb', line 61

def started?
  state == :started
end

.stopObject



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

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)


65
66
67
# File 'lib/aether_observatory/observer_base.rb', line 65

def stopped?
  state == :stopped
end

.subscribe_to(topic) ⇒ Object



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

def subscribe_to(topic)
  subscribed_topics.add(topic)

  return if stopped?

  register_subscription_to(topic)
end

.subscribed_toObject



57
58
59
# File 'lib/aether_observatory/observer_base.rb', line 57

def subscribed_to
  subscribed_topics.to_a
end

.unsubscribe_from(topic) ⇒ Object



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

def unsubscribe_from(topic)
  subscribed_topics.delete(topic)

  return if stopped?

  unregister_subscription_to(topic)
end