Class: Shouter::Store
- Inherits:
-
Object
- Object
- Shouter::Store
- Defined in:
- lib/shouter/store.rb
Constant Summary collapse
- @@listeners =
[]
- @@mutex =
Mutex.new
Class Method Summary collapse
- .clear ⇒ Object
- .inherited(subclass) ⇒ Object
- .listeners ⇒ Object
- .mutex ⇒ Object
- .notify(scope, event, args) ⇒ Object
- .register(objects, options) ⇒ Object
- .unregister(objects) ⇒ Object
Class Method Details
.clear ⇒ Object
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 |
.listeners ⇒ Object
48 49 50 |
# File 'lib/shouter/store.rb', line 48 def listeners @@listeners end |
.mutex ⇒ Object
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, ) mutex.synchronize do objects.each { |object| @@listeners << Shouter::Listener.new(object, ) } 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 |