u-observers 0.8.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.tool-versions +1 -0
- data/.travis.sh +34 -0
- data/.travis.yml +27 -3
- data/Gemfile +31 -5
- data/README.md +495 -11
- data/README.pt-BR.md +529 -0
- data/lib/micro/observers.rb +5 -23
- data/lib/micro/observers/broadcast.rb +73 -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
- data/test.sh +11 -0
- data/u-observers.gemspec +5 -1
- metadata +44 -5
- data/lib/micro/observers/events_or_actions.rb +0 -25
- data/lib/micro/observers/manager.rb +0 -101
data/test.sh
ADDED
data/u-observers.gemspec
CHANGED
@@ -10,7 +10,6 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.description = %q{Simple and powerful implementation of the observer pattern.}
|
11
11
|
spec.homepage = 'https://github.com/serradura/u-observers'
|
12
12
|
spec.license = 'MIT'
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.2.0')
|
14
13
|
|
15
14
|
spec.metadata['homepage_uri'] = spec.homepage
|
16
15
|
spec.metadata['source_code_uri'] = 'https://github.com/serradura/u-observers'
|
@@ -24,4 +23,9 @@ Gem::Specification.new do |spec|
|
|
24
23
|
spec.bindir = 'exe'
|
25
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
25
|
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.2.0')
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler'
|
30
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
27
31
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.2.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-
|
12
|
-
dependencies:
|
11
|
+
date: 2020-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
13
41
|
description: Simple and powerful implementation of the observer pattern.
|
14
42
|
email:
|
15
43
|
- rodrigo.serradura@gmail.com
|
@@ -18,20 +46,31 @@ extensions: []
|
|
18
46
|
extra_rdoc_files: []
|
19
47
|
files:
|
20
48
|
- ".gitignore"
|
49
|
+
- ".tool-versions"
|
50
|
+
- ".travis.sh"
|
21
51
|
- ".travis.yml"
|
22
52
|
- CODE_OF_CONDUCT.md
|
23
53
|
- Gemfile
|
24
54
|
- LICENSE.txt
|
25
55
|
- README.md
|
56
|
+
- README.pt-BR.md
|
26
57
|
- Rakefile
|
27
58
|
- bin/console
|
28
59
|
- bin/setup
|
29
60
|
- lib/micro/observers.rb
|
30
|
-
- lib/micro/observers/
|
31
|
-
- 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
|
32
68
|
- lib/micro/observers/utils.rb
|
33
69
|
- lib/micro/observers/version.rb
|
34
70
|
- lib/u-observers.rb
|
71
|
+
- lib/u-observers/for/active_model.rb
|
72
|
+
- lib/u-observers/for/active_record.rb
|
73
|
+
- test.sh
|
35
74
|
- u-observers.gemspec
|
36
75
|
homepage: https://github.com/serradura/u-observers
|
37
76
|
licenses:
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
module Observers
|
5
|
-
|
6
|
-
module EventsOrActions
|
7
|
-
DEFAULTS = [:call]
|
8
|
-
|
9
|
-
def self.[](value)
|
10
|
-
values = Utils.compact_array(value)
|
11
|
-
|
12
|
-
values.empty? ? DEFAULTS : values
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.fetch_actions(hash)
|
16
|
-
return self[hash[: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
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
module Observers
|
5
|
-
|
6
|
-
class Manager
|
7
|
-
EqualTo = -> (observer) do
|
8
|
-
-> item { item[0] == :observer && item[1] == observer }
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.for(subject)
|
12
|
-
new(subject)
|
13
|
-
end
|
14
|
-
|
15
|
-
def initialize(subject, list = nil)
|
16
|
-
@subject = subject
|
17
|
-
|
18
|
-
@list = Utils.compact_array(list.kind_of?(Array) ? list : [])
|
19
|
-
end
|
20
|
-
|
21
|
-
def included?(observer)
|
22
|
-
@list.any?(&EqualTo[observer])
|
23
|
-
end
|
24
|
-
|
25
|
-
def attach(*args)
|
26
|
-
options = args.last.is_a?(Hash) ? args.pop : Utils::EMPTY_HASH
|
27
|
-
|
28
|
-
Utils.compact_array(args).each do |observer|
|
29
|
-
if options[:allow_duplication] || !included?(observer)
|
30
|
-
@list << [:observer, observer, options[:data]]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
self
|
35
|
-
end
|
36
|
-
|
37
|
-
def on(options = Utils::EMPTY_HASH)
|
38
|
-
event, callable, with = options[:event], options[:call], options[:with]
|
39
|
-
|
40
|
-
return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
|
41
|
-
|
42
|
-
@list << [:callable, event, [callable, with]]
|
43
|
-
end
|
44
|
-
|
45
|
-
def detach(*args)
|
46
|
-
Utils.compact_array(args).each do |observer|
|
47
|
-
@list.delete_if(&EqualTo[observer])
|
48
|
-
end
|
49
|
-
|
50
|
-
self
|
51
|
-
end
|
52
|
-
|
53
|
-
def notify(*events)
|
54
|
-
broadcast(EventsOrActions[events])
|
55
|
-
|
56
|
-
self
|
57
|
-
end
|
58
|
-
|
59
|
-
def call(options = Utils::EMPTY_HASH)
|
60
|
-
broadcast(EventsOrActions.fetch_actions(options))
|
61
|
-
|
62
|
-
self
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def broadcast(evts_or_acts)
|
68
|
-
evts_or_acts.each do |evt_or_act|
|
69
|
-
@list.each do |strategy, observer, data|
|
70
|
-
call!(observer, strategy, data, with: evt_or_act)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def call!(observer, strategy, data, with:)
|
76
|
-
return call_callable(data) if strategy == :callable && observer == with
|
77
|
-
|
78
|
-
return call_observer(observer, with, data) if strategy == :observer
|
79
|
-
end
|
80
|
-
|
81
|
-
def call_callable(data)
|
82
|
-
callable, arg = data[0], data[1]
|
83
|
-
|
84
|
-
callable_arg = arg.is_a?(Proc) ? arg.call(@subject) : (arg || @subject)
|
85
|
-
|
86
|
-
callable.call(callable_arg)
|
87
|
-
end
|
88
|
-
|
89
|
-
def call_observer(observer, method_name, data)
|
90
|
-
return unless observer.respond_to?(method_name)
|
91
|
-
|
92
|
-
handler = observer.method(method_name)
|
93
|
-
|
94
|
-
handler.arity == 1 ? handler.call(@subject) : handler.call(@subject, data)
|
95
|
-
end
|
96
|
-
|
97
|
-
private_constant :EqualTo
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
end
|