Class: Houston::Observer
- Inherits:
-
Object
show all
- Defined in:
- lib/houston/boot/observer.rb
Defined Under Namespace
Classes: Callback, CallbackOnce, MissingParamError, UnregisteredEventError, UnregisteredParamError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Observer.
9
10
11
12
|
# File 'lib/houston/boot/observer.rb', line 9
def initialize
@async = true
clear!
end
|
Instance Attribute Details
#async ⇒ Object
Returns the value of attribute async.
3
4
5
|
# File 'lib/houston/boot/observer.rb', line 3
def async
@async
end
|
Instance Method Details
#clear! ⇒ Object
62
63
64
|
# File 'lib/houston/boot/observer.rb', line 62
def clear!
@observers = {}
end
|
#fire(event, params = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/houston/boot/observer.rb', line 41
def fire(event, params={})
assert_registered! event
unless params.is_a?(Hash)
raise ArgumentError, "params must be a Hash" unless params.respond_to?(:to_h)
params = params.to_h
end
assert_registered_params! event, params
assert_serializable! params
params = ReadonlyHash.new(params)
observers_of(event).each do |callback|
callback.call params
end
observers_of(:*).each do |callback|
callback.call event, params
end
nil
end
|
#observed?(event) ⇒ Boolean
36
37
38
39
|
# File 'lib/houston/boot/observer.rb', line 36
def observed?(event)
assert_registered! event
observers_of(event).any?
end
|
#off(callback, &block) ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/houston/boot/observer.rb', line 26
def off(callback, &block)
if block_given?
event = callback
callback = observers_of(event).detect { |callback| callback.block == block }
return nil unless callback
end
observers_of(callback.event).delete callback
nil
end
|
#on(event, options = {}, &block) ⇒ Object
14
15
16
17
18
|
# File 'lib/houston/boot/observer.rb', line 14
def on(event, options={}, &block)
assert_registered! event
observers_of(event).push Callback.new(self, event, options, block)
nil
end
|
#once(event, options = {}, &block) ⇒ Object
20
21
22
23
24
|
# File 'lib/houston/boot/observer.rb', line 20
def once(event, options={}, &block)
assert_registered! event
observers_of(event).push CallbackOnce.new(self, event, options, block)
nil
end
|