u-observers 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/micro/observers.rb +1 -1
- data/lib/micro/observers/manager.rb +30 -11
- data/lib/micro/observers/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fe383e800461aeb658a9b17f2d4799b194b7e70fdf5a545dbe07f69cd9d2642
|
4
|
+
data.tar.gz: a42fdd9868c43855006b763faa86b0377ae2204f6e9c20893edfbadf49792a60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af8ce345aec2b28cef954f6de09ce835d1e4236e115877b04c92227121a85ebedd1b1cf422e3cc1645f2b7ad818cfdf81d9b05bcdb41007bca4fd9115d900fc0
|
7
|
+
data.tar.gz: 45ade4eefd27ae3b8f0aed723681b5c1931cd11c34f78f17a21e59aa745a67de27b32272f4c01377950420f4dd0743c84258b9415cf24dea891d253f9ca1fe4b
|
data/lib/micro/observers.rb
CHANGED
@@ -6,39 +6,58 @@ module Micro
|
|
6
6
|
class Manager
|
7
7
|
EMPTY_HASH = {}.freeze
|
8
8
|
|
9
|
+
SameObserver = -> (observer) do
|
10
|
+
-> item { item[0] == :observer && item[1] == observer }
|
11
|
+
end
|
12
|
+
|
9
13
|
def self.for(subject)
|
10
14
|
new(subject)
|
11
15
|
end
|
12
16
|
|
13
17
|
def initialize(subject, list = nil)
|
14
18
|
@subject = subject
|
19
|
+
|
15
20
|
@list = (list.kind_of?(Array) ? list : []).flatten.tap(&:compact!)
|
16
21
|
end
|
17
22
|
|
23
|
+
def included?(observer)
|
24
|
+
@list.any?(&SameObserver[observer])
|
25
|
+
end
|
26
|
+
|
18
27
|
def attach(observer, options = EMPTY_HASH)
|
19
|
-
if options[:allow_duplication] ||
|
20
|
-
@list << [observer, options[:data]]
|
28
|
+
if options[:allow_duplication] || !included?(observer)
|
29
|
+
@list << [:observer, observer, options[:data]]
|
21
30
|
end
|
22
31
|
|
23
32
|
self
|
24
33
|
end
|
25
34
|
|
35
|
+
def on(options=EMPTY_HASH)
|
36
|
+
action, callable, with = options[:action], options[:call], options[:with]
|
37
|
+
|
38
|
+
return self unless action.is_a?(Symbol) && callable.respond_to?(:call)
|
39
|
+
|
40
|
+
arg = with.is_a?(Proc) ? with.call(@subject) : subject
|
41
|
+
|
42
|
+
@list << [:caller, action, [callable, arg]]
|
43
|
+
end
|
44
|
+
|
26
45
|
def detach(observer)
|
27
|
-
@list.delete_if
|
46
|
+
@list.delete_if(&SameObserver[observer])
|
28
47
|
|
29
48
|
self
|
30
49
|
end
|
31
50
|
|
32
|
-
def call(action
|
33
|
-
@list.each do |observer, data|
|
34
|
-
|
51
|
+
def call(action = :call)
|
52
|
+
@list.each do |type, observer, data|
|
53
|
+
if type == :caller && observer == action
|
54
|
+
data[0].call(data[1])
|
55
|
+
elsif type == :observer && observer.respond_to?(action)
|
56
|
+
handler = observer.method(action)
|
35
57
|
|
36
|
-
|
58
|
+
return handler.call(@subject) if handler.arity == 1
|
37
59
|
|
38
|
-
|
39
|
-
when 2 then handler.call(@subject, data)
|
40
|
-
when 1 then handler.call(@subject)
|
41
|
-
else raise NotImplementedError
|
60
|
+
handler.call(@subject, data)
|
42
61
|
end
|
43
62
|
end
|
44
63
|
|