u-observers 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2268ebc8924bf446ca73f3b1fbdb0cb4a8fba9f60eb01173e68fa7486747b1ed
|
4
|
+
data.tar.gz: 329fad95d53450a550c9fc964fec4bb3732f5a63d684835f0ef6d766df839d15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3a2f1895221f171ca6499bca6284a9d349e77b9d6661b41a0b15c662d9f400af86d7d0b77e4331ca9a985b6db2d6628415be5422944cb09aba32d676aa537eb
|
7
|
+
data.tar.gz: 82daa067cd2cf85c3b00c0aca546131e38c1ee365336d5f01cb868b81d7b91be806ecb606c61ad5e56d2364f9341f8636d093f02d6b6f424ddccc0702b11f7e7
|
data/lib/micro/observers.rb
CHANGED
@@ -8,15 +8,15 @@ module Micro
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def notify_observers!(with:)
|
11
|
-
proc { |object| with.each { |evt_or_act| object.observers.
|
11
|
+
proc { |object| with.each { |evt_or_act| object.observers.notify(evt_or_act) } }
|
12
12
|
end
|
13
13
|
|
14
14
|
def notify_observers(*events)
|
15
15
|
notify_observers!(with: EventsOrActions[events])
|
16
16
|
end
|
17
17
|
|
18
|
-
def call_observers(
|
19
|
-
notify_observers!(with: EventsOrActions
|
18
|
+
def call_observers(options = Utils::EMPTY_HASH)
|
19
|
+
notify_observers!(with: EventsOrActions.fetch_actions(options))
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -4,15 +4,21 @@ module Micro
|
|
4
4
|
module Observers
|
5
5
|
|
6
6
|
module EventsOrActions
|
7
|
-
|
7
|
+
DEFAULTS = [:call]
|
8
8
|
|
9
|
-
def self.[](
|
10
|
-
|
9
|
+
def self.[](value)
|
10
|
+
values = Utils.compact_array(value)
|
11
11
|
|
12
|
-
|
12
|
+
values.empty? ? DEFAULTS : values
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
def self.fetch_actions(hash)
|
16
|
+
return self[hash.fetch(:actions) { hash.fetch(:action) }] if hash.is_a?(Hash)
|
17
|
+
|
18
|
+
raise ArgumentError, 'expected a hash with the key :action or :actions'
|
19
|
+
end
|
20
|
+
|
21
|
+
private_constant :DEFAULTS
|
16
22
|
end
|
17
23
|
|
18
24
|
end
|
@@ -4,8 +4,6 @@ module Micro
|
|
4
4
|
module Observers
|
5
5
|
|
6
6
|
class Manager
|
7
|
-
EMPTY_HASH = {}.freeze
|
8
|
-
|
9
7
|
EqualTo = -> (observer) do
|
10
8
|
-> item { item[0] == :observer && item[1] == observer }
|
11
9
|
end
|
@@ -24,7 +22,7 @@ module Micro
|
|
24
22
|
@list.any?(&EqualTo[observer])
|
25
23
|
end
|
26
24
|
|
27
|
-
def attach(observer, options = EMPTY_HASH)
|
25
|
+
def attach(observer, options = Utils::EMPTY_HASH)
|
28
26
|
if options[:allow_duplication] || !included?(observer)
|
29
27
|
@list << [:observer, observer, options[:data]]
|
30
28
|
end
|
@@ -32,7 +30,7 @@ module Micro
|
|
32
30
|
self
|
33
31
|
end
|
34
32
|
|
35
|
-
def on(options=EMPTY_HASH)
|
33
|
+
def on(options = Utils::EMPTY_HASH)
|
36
34
|
event, callable, with = options[:event], options[:call], options[:with]
|
37
35
|
|
38
36
|
return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
|
@@ -48,14 +46,14 @@ module Micro
|
|
48
46
|
self
|
49
47
|
end
|
50
48
|
|
51
|
-
def notify(
|
52
|
-
notify!(
|
49
|
+
def notify(*events)
|
50
|
+
EventsOrActions[events].each { |act| notify!(act) }
|
53
51
|
|
54
52
|
self
|
55
53
|
end
|
56
54
|
|
57
|
-
def call(
|
58
|
-
EventsOrActions
|
55
|
+
def call(options = Utils::EMPTY_HASH)
|
56
|
+
EventsOrActions.fetch_actions(options).each { |act| notify!(act) }
|
59
57
|
|
60
58
|
self
|
61
59
|
end
|
@@ -80,7 +78,7 @@ module Micro
|
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
83
|
-
private_constant :
|
81
|
+
private_constant :EqualTo
|
84
82
|
end
|
85
83
|
|
86
84
|
end
|