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



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



44
45
46
# File 'lib/shouter/store.rb', line 44

def listeners
  @@listeners
end

.mutexObject



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

def mutex
  @@mutex
end

.notify(scope, event, args, &block) ⇒ Object



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

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

  listeners.select { |listener| listener.for?(scope) }.each do |listener|
    listener.notify(event, args, &block)
  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