u-observers 0.3.0 → 0.4.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 +4 -4
- data/lib/micro/observers.rb +11 -7
- data/lib/micro/observers/events_or_actions.rb +19 -0
- data/lib/micro/observers/manager.rb +34 -20
- data/lib/micro/observers/utils.rb +13 -0
- data/lib/micro/observers/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9c81561b00ca412faa80abc349e8a87251f4d84ce81da5fcd95ca1117e2bd2e
|
4
|
+
data.tar.gz: da923ace22c60cac08f710dccac65367b0bb568d9b4a99fbbe8bc164c8824b12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69eedf04951d55a0889412d50e62e226fdfe2561a3a3a52df087f0091ec15ec696fe002fe906a00fdf15119455c4c7f7f1a54a464c83e56c3ec5d6ff143ed1f4
|
7
|
+
data.tar.gz: 805003dac964746271f6b7789527e608fbd2caf77dd8f9eb735b8d30f42159e9d577f0a8bb0b3b60199fc764b40c0ae7a1edd005deb2d01f7e9567d49e18c4fd
|
data/lib/micro/observers.rb
CHANGED
@@ -2,23 +2,27 @@ require 'micro/observers/version'
|
|
2
2
|
|
3
3
|
module Micro
|
4
4
|
module Observers
|
5
|
+
require 'micro/observers/utils'
|
6
|
+
require 'micro/observers/events_or_actions'
|
5
7
|
require 'micro/observers/manager'
|
6
8
|
|
7
9
|
module ClassMethods
|
8
|
-
def
|
9
|
-
proc
|
10
|
-
Array(with)
|
11
|
-
.each { |action| object.observers.call(action) }
|
12
|
-
end
|
10
|
+
def notify_observers!(with:)
|
11
|
+
proc { |object| with.each { |evt_or_act| object.observers.call(evt_or_act) } }
|
13
12
|
end
|
14
13
|
|
15
|
-
def notify_observers(
|
16
|
-
|
14
|
+
def notify_observers(*events)
|
15
|
+
notify_observers!(with: EventsOrActions[events])
|
16
|
+
end
|
17
|
+
|
18
|
+
def call_observers(action: :call)
|
19
|
+
notify_observers!(with: EventsOrActions[action])
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
20
23
|
def self.included(base)
|
21
24
|
base.extend(ClassMethods)
|
25
|
+
base.send(:private_class_method, :notify_observers!)
|
22
26
|
end
|
23
27
|
|
24
28
|
def observers
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Observers
|
5
|
+
|
6
|
+
module EventsOrActions
|
7
|
+
DEFAULT = [:call]
|
8
|
+
|
9
|
+
def self.[](values)
|
10
|
+
vals = Utils.compact_array(values)
|
11
|
+
|
12
|
+
vals.empty? ? DEFAULT : vals
|
13
|
+
end
|
14
|
+
|
15
|
+
private_constant :DEFAULT
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -6,7 +6,7 @@ module Micro
|
|
6
6
|
class Manager
|
7
7
|
EMPTY_HASH = {}.freeze
|
8
8
|
|
9
|
-
|
9
|
+
EqualTo = -> (observer) do
|
10
10
|
-> item { item[0] == :observer && item[1] == observer }
|
11
11
|
end
|
12
12
|
|
@@ -17,11 +17,11 @@ module Micro
|
|
17
17
|
def initialize(subject, list = nil)
|
18
18
|
@subject = subject
|
19
19
|
|
20
|
-
@list = (list.kind_of?(Array) ? list : [])
|
20
|
+
@list = Utils.compact_array(list.kind_of?(Array) ? list : [])
|
21
21
|
end
|
22
22
|
|
23
23
|
def included?(observer)
|
24
|
-
@list.any?(&
|
24
|
+
@list.any?(&EqualTo[observer])
|
25
25
|
end
|
26
26
|
|
27
27
|
def attach(observer, options = EMPTY_HASH)
|
@@ -33,40 +33,54 @@ module Micro
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def on(options=EMPTY_HASH)
|
36
|
-
|
36
|
+
event, callable, with = options[:event], options[:call], options[:with]
|
37
37
|
|
38
|
-
return self unless
|
38
|
+
return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
|
39
39
|
|
40
|
-
arg = with.is_a?(Proc) ? with.call(@subject) : subject
|
40
|
+
arg = with.is_a?(Proc) ? with.call(@subject) : (arg || subject)
|
41
41
|
|
42
|
-
@list << [:
|
42
|
+
@list << [:callable, event, [callable, arg]]
|
43
43
|
end
|
44
44
|
|
45
45
|
def detach(observer)
|
46
|
-
@list.delete_if(&
|
46
|
+
@list.delete_if(&EqualTo[observer])
|
47
47
|
|
48
48
|
self
|
49
49
|
end
|
50
50
|
|
51
|
-
def
|
52
|
-
|
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)
|
51
|
+
def notify(event)
|
52
|
+
notify!(event)
|
57
53
|
|
58
|
-
|
54
|
+
self
|
55
|
+
end
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
end
|
57
|
+
def call(*actions)
|
58
|
+
EventsOrActions[actions].each { |act| notify!(act) }
|
63
59
|
|
64
60
|
self
|
65
61
|
end
|
66
62
|
|
67
|
-
|
63
|
+
private
|
64
|
+
|
65
|
+
def notify!(evt_or_act)
|
66
|
+
@list.each do |strategy, observer, data|
|
67
|
+
call!(observer, strategy, data, with: evt_or_act)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def call!(observer, strategy, data, with:)
|
72
|
+
return data[0].call(data[1]) if strategy == :callable && observer == with
|
73
|
+
|
74
|
+
if strategy == :observer && observer.respond_to?(with)
|
75
|
+
handler = observer.method(with)
|
76
|
+
|
77
|
+
return handler.call(@subject) if handler.arity == 1
|
78
|
+
|
79
|
+
handler.call(@subject, data)
|
80
|
+
end
|
81
|
+
end
|
68
82
|
|
69
|
-
private_constant :EMPTY_HASH
|
83
|
+
private_constant :EMPTY_HASH, :EqualTo
|
70
84
|
end
|
71
85
|
|
72
86
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simple and powerful implementation of the observer pattern.
|
14
14
|
email:
|
@@ -27,7 +27,9 @@ files:
|
|
27
27
|
- bin/console
|
28
28
|
- bin/setup
|
29
29
|
- lib/micro/observers.rb
|
30
|
+
- lib/micro/observers/events_or_actions.rb
|
30
31
|
- lib/micro/observers/manager.rb
|
32
|
+
- lib/micro/observers/utils.rb
|
31
33
|
- lib/micro/observers/version.rb
|
32
34
|
- lib/u-observers.rb
|
33
35
|
- u-observers.gemspec
|