Class: FiniteMachine::Subscribers

Inherits:
Object
  • Object
show all
Includes:
Enumerable, MonitorMixin
Defined in:
lib/finite_machine/subscribers.rb

Overview

A class responsibile for storage of event subscribers

Instance Method Summary collapse

Constructor Details

#initializeSubscribers

Initialize a subscribers collection



14
15
16
17
# File 'lib/finite_machine/subscribers.rb', line 14

def initialize
  super
  @subscribers = []
end

Instance Method Details

#each(&block) ⇒ Object

Iterate over subscribers



22
23
24
# File 'lib/finite_machine/subscribers.rb', line 22

def each(&block)
  @subscribers.each(&block)
end

#empty?Boolean

Check if anyone is subscribed

Returns:

  • (Boolean)


38
39
40
# File 'lib/finite_machine/subscribers.rb', line 38

def empty?
  @subscribers.empty?
end

#index(subscriber) ⇒ Object

Return index of the subscriber



29
30
31
# File 'lib/finite_machine/subscribers.rb', line 29

def index(subscriber)
  @subscribers.index(subscriber)
end

#resetself

Reset subscribers

Returns:

  • (self)


83
84
85
86
# File 'lib/finite_machine/subscribers.rb', line 83

def reset
  @subscribers.clear
  self
end

#sizeInteger

Number of subscribed listeners

Returns:

  • (Integer)


74
75
76
# File 'lib/finite_machine/subscribers.rb', line 74

def size
  synchronize { @subscribers.size }
end

#subscribe(*observers) ⇒ undefined

Add listener to subscribers

Parameters:

  • observers (Array[#trigger])

Returns:

  • (undefined)


49
50
51
52
53
# File 'lib/finite_machine/subscribers.rb', line 49

def subscribe(*observers)
  synchronize do
    observers.each { |observer| @subscribers << observer }
  end
end

#visit(hook_event, *data) ⇒ undefined

Visit subscribers and notify

Parameters:

  • hook_event (HookEvent)

    the callback event to notify about

Returns:

  • (undefined)


63
64
65
66
67
# File 'lib/finite_machine/subscribers.rb', line 63

def visit(hook_event, *data)
  each { |subscriber|
    synchronize { hook_event.notify(subscriber, *data) }
  }
end