Class: Shouter::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/shouter/store.rb

Constant Summary collapse

@@listeners =
[]
@@mutex =
Mutex.new

Class Method Summary collapse

Class Method Details

.clearObject



30
31
32
33
34
# File 'lib/shouter/store.rb', line 30

def clear
  mutex.synchronize do
    @@listeners = []
  end
end

.inherited(subclass) ⇒ Object

Raises:



14
15
16
# File 'lib/shouter/store.rb', line 14

def inherited(subclass)
  raise NoInheritanceAllowedError.new("#{self.class.to_s} is meant to be a singleton class and not to be inherited")
end

.listenersObject



48
49
50
# File 'lib/shouter/store.rb', line 48

def listeners
  @@listeners
end

.mutexObject



52
53
54
# File 'lib/shouter/store.rb', line 52

def mutex
  @@mutex
end

.notify(scope, event, args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shouter/store.rb', line 36

def notify(scope, event, args)
  return if listeners.empty?

  listeners.select { |listener| listener.for?(scope) }.each do |listener|
    klass = listener.object
    klass.public_send(event, *args) if klass.respond_to?(event)
    # Serves as callback
    yield if block_given?
    unregister(klass) if listener.single?
  end
end

.register(objects, options) ⇒ Object



18
19
20
21
22
# File 'lib/shouter/store.rb', line 18

def register(objects, options)
  mutex.synchronize do
    objects.each { |object| @@listeners << Shouter::Listener.new(object, options) }
  end
end

.unregister(objects) ⇒ Object



24
25
26
27
28
# File 'lib/shouter/store.rb', line 24

def unregister(objects)
  mutex.synchronize do
    [*objects].each { |object| listeners.delete_if { |listener| listener.object == object } }
  end
end