u-observers 0.9.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.sh +3 -0
- data/Gemfile +10 -8
- data/README.md +495 -11
- data/README.pt-BR.md +529 -0
- data/lib/micro/observers.rb +5 -22
- data/lib/micro/observers/broadcast.rb +77 -0
- data/lib/micro/observers/event.rb +21 -0
- data/lib/micro/observers/event/names.rb +29 -0
- data/lib/micro/observers/for/active_model.rb +36 -0
- data/lib/micro/observers/for/active_record.rb +16 -0
- data/lib/micro/observers/set.rb +100 -0
- data/lib/micro/observers/subscribers.rb +110 -0
- data/lib/micro/observers/utils.rb +12 -2
- data/lib/micro/observers/version.rb +1 -1
- data/lib/u-observers/for/active_model.rb +2 -0
- data/lib/u-observers/for/active_record.rb +2 -0
- metadata +12 -4
- data/lib/micro/observers/events.rb +0 -19
- data/lib/micro/observers/manager.rb +0 -155
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:
|
4
|
+
version: 2.2.1
|
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-
|
11
|
+
date: 2020-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,15 +53,23 @@ files:
|
|
53
53
|
- Gemfile
|
54
54
|
- LICENSE.txt
|
55
55
|
- README.md
|
56
|
+
- README.pt-BR.md
|
56
57
|
- Rakefile
|
57
58
|
- bin/console
|
58
59
|
- bin/setup
|
59
60
|
- lib/micro/observers.rb
|
60
|
-
- lib/micro/observers/
|
61
|
-
- lib/micro/observers/
|
61
|
+
- lib/micro/observers/broadcast.rb
|
62
|
+
- lib/micro/observers/event.rb
|
63
|
+
- lib/micro/observers/event/names.rb
|
64
|
+
- lib/micro/observers/for/active_model.rb
|
65
|
+
- lib/micro/observers/for/active_record.rb
|
66
|
+
- lib/micro/observers/set.rb
|
67
|
+
- lib/micro/observers/subscribers.rb
|
62
68
|
- lib/micro/observers/utils.rb
|
63
69
|
- lib/micro/observers/version.rb
|
64
70
|
- lib/u-observers.rb
|
71
|
+
- lib/u-observers/for/active_model.rb
|
72
|
+
- lib/u-observers/for/active_record.rb
|
65
73
|
- test.sh
|
66
74
|
- u-observers.gemspec
|
67
75
|
homepage: https://github.com/serradura/u-observers
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
module Observers
|
5
|
-
|
6
|
-
module Events
|
7
|
-
DEFAULTS = [:call]
|
8
|
-
|
9
|
-
def self.[](value)
|
10
|
-
values = Utils.compact_array(value)
|
11
|
-
|
12
|
-
values.empty? ? DEFAULTS : values
|
13
|
-
end
|
14
|
-
|
15
|
-
private_constant :DEFAULTS
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
@@ -1,155 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
module Observers
|
5
|
-
|
6
|
-
class Manager
|
7
|
-
MapObserver = -> (observer, options) { [:observer, observer, options[:context]] }
|
8
|
-
|
9
|
-
MapSubscribers = -> (value) do
|
10
|
-
array = Utils.compact_array(value.kind_of?(Array) ? value : [])
|
11
|
-
array.map { |observer| MapObserver[observer, Utils::EMPTY_HASH] }
|
12
|
-
end
|
13
|
-
|
14
|
-
EqualTo = -> (observer) do
|
15
|
-
-> subscriber do
|
16
|
-
handler = subscriber[0] == :observer ? subscriber[1] : subscriber[2][0]
|
17
|
-
handler == observer
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.for(subject)
|
22
|
-
new(subject)
|
23
|
-
end
|
24
|
-
|
25
|
-
def initialize(subject, subscribers: nil)
|
26
|
-
@subject = subject
|
27
|
-
|
28
|
-
@subject_changed = false
|
29
|
-
|
30
|
-
@subscribers = MapSubscribers.call(subscribers)
|
31
|
-
end
|
32
|
-
|
33
|
-
def count
|
34
|
-
@subscribers.size
|
35
|
-
end
|
36
|
-
|
37
|
-
def none?
|
38
|
-
@subscribers.empty?
|
39
|
-
end
|
40
|
-
|
41
|
-
def some?
|
42
|
-
!none?
|
43
|
-
end
|
44
|
-
|
45
|
-
def subject_changed?
|
46
|
-
@subject_changed
|
47
|
-
end
|
48
|
-
|
49
|
-
INVALID_BOOLEAN_ERROR = 'expected a boolean (true, false)'.freeze
|
50
|
-
|
51
|
-
def subject_changed(state)
|
52
|
-
if state == true || state == false
|
53
|
-
@subject_changed = state
|
54
|
-
|
55
|
-
return self
|
56
|
-
end
|
57
|
-
|
58
|
-
raise ArgumentError, INVALID_BOOLEAN_ERROR
|
59
|
-
end
|
60
|
-
|
61
|
-
def subject_changed!
|
62
|
-
subject_changed(true)
|
63
|
-
end
|
64
|
-
|
65
|
-
def included?(observer)
|
66
|
-
@subscribers.any?(&EqualTo[observer])
|
67
|
-
end
|
68
|
-
|
69
|
-
def attach(*args)
|
70
|
-
options = args.last.is_a?(Hash) ? args.pop : Utils::EMPTY_HASH
|
71
|
-
|
72
|
-
Utils.compact_array(args).each do |observer|
|
73
|
-
@subscribers << MapObserver[observer, options] unless included?(observer)
|
74
|
-
end
|
75
|
-
|
76
|
-
self
|
77
|
-
end
|
78
|
-
|
79
|
-
def detach(*args)
|
80
|
-
Utils.compact_array(args).each do |observer|
|
81
|
-
@subscribers.delete_if(&EqualTo[observer])
|
82
|
-
end
|
83
|
-
|
84
|
-
self
|
85
|
-
end
|
86
|
-
|
87
|
-
def on(options = Utils::EMPTY_HASH)
|
88
|
-
event, callable, with = options[:event], options[:call], options[:with]
|
89
|
-
|
90
|
-
return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
|
91
|
-
|
92
|
-
@subscribers << [:callable, event, [callable, with]] unless included?(callable)
|
93
|
-
|
94
|
-
self
|
95
|
-
end
|
96
|
-
|
97
|
-
def notify(*events)
|
98
|
-
notify!(Events[events])
|
99
|
-
end
|
100
|
-
|
101
|
-
def call(*events)
|
102
|
-
broadcast(Events[events])
|
103
|
-
|
104
|
-
self
|
105
|
-
end
|
106
|
-
|
107
|
-
protected
|
108
|
-
|
109
|
-
def notify!(events)
|
110
|
-
return self unless subject_changed?
|
111
|
-
|
112
|
-
broadcast(events)
|
113
|
-
|
114
|
-
subject_changed(false)
|
115
|
-
|
116
|
-
self
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def broadcast(events)
|
122
|
-
events.each do |event|
|
123
|
-
@subscribers.each { |subscriber| call!(subscriber, event) }
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def call!(subscriber, event)
|
128
|
-
strategy, observer, context = subscriber
|
129
|
-
|
130
|
-
return call_observer(observer, event, context) if strategy == :observer
|
131
|
-
|
132
|
-
return call_callable(context) if strategy == :callable && observer == event
|
133
|
-
end
|
134
|
-
|
135
|
-
def call_callable(context)
|
136
|
-
callable, with = context[0], context[1]
|
137
|
-
|
138
|
-
arg = with.is_a?(Proc) ? with.call(@subject) : (with || @subject)
|
139
|
-
|
140
|
-
callable.call(arg)
|
141
|
-
end
|
142
|
-
|
143
|
-
def call_observer(observer, method_name, context)
|
144
|
-
return unless observer.respond_to?(method_name)
|
145
|
-
|
146
|
-
handler = observer.is_a?(Proc) ? observer : observer.method(method_name)
|
147
|
-
|
148
|
-
handler.arity == 1 ? handler.call(@subject) : handler.call(@subject, context)
|
149
|
-
end
|
150
|
-
|
151
|
-
private_constant :MapObserver, :MapSubscribers, :EqualTo, :INVALID_BOOLEAN_ERROR
|
152
|
-
end
|
153
|
-
|
154
|
-
end
|
155
|
-
end
|