u-observers 0.8.0 → 0.9.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 +31 -0
- data/.travis.yml +27 -3
- data/Gemfile +29 -5
- data/lib/micro/observers.rb +7 -8
- data/lib/micro/observers/{events_or_actions.rb → events.rb} +1 -7
- data/lib/micro/observers/manager.rb +89 -35
- data/lib/micro/observers/version.rb +1 -1
- data/test.sh +11 -0
- data/u-observers.gemspec +5 -1
- metadata +35 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfa6d5600e86dd6086b2d6ebd4cd1b015ebe3331bd4cd61be90f2d96a1abf601
|
4
|
+
data.tar.gz: b483b0721def3d41085260343edf1041905b376264354b6ffbd07cbb5386725f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '06318ee21d6b9e842fa7ad752f9ccb137286667211fc43bed267d5593506d239fe946f03159236e00e0549c45e71d5de1f36cb1213ce698c3ad3b8edaf4c5abf'
|
7
|
+
data.tar.gz: 91aac7ae90a990f5575838bf13ddfd174066257a2b82620e82fc8af963519d9c85526aa742005b3d7c1cc78c1dba67d557f62ab233d34b30b70b8c69a945a929
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.6.5
|
data/.travis.sh
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
ruby_v=$(ruby -v)
|
4
|
+
|
5
|
+
ACTIVERECORD_VERSION='3.2' bundle update
|
6
|
+
ACTIVERECORD_VERSION='3.2' bundle exec rake test
|
7
|
+
|
8
|
+
ACTIVERECORD_VERSION='4.0' bundle update
|
9
|
+
ACTIVERECORD_VERSION='4.0' bundle exec rake test
|
10
|
+
|
11
|
+
ACTIVERECORD_VERSION='4.1' bundle update
|
12
|
+
ACTIVERECORD_VERSION='4.1' bundle exec rake test
|
13
|
+
|
14
|
+
ACTIVERECORD_VERSION='4.2' bundle update
|
15
|
+
ACTIVERECORD_VERSION='4.2' bundle exec rake test
|
16
|
+
|
17
|
+
ACTIVERECORD_VERSION='5.0' bundle update
|
18
|
+
ACTIVERECORD_VERSION='5.0' bundle exec rake test
|
19
|
+
|
20
|
+
ACTIVERECORD_VERSION='5.1' bundle update
|
21
|
+
ACTIVERECORD_VERSION='5.1' bundle exec rake test
|
22
|
+
|
23
|
+
if [[ ! $ruby_v =~ '2.2.0' ]]; then
|
24
|
+
ACTIVERECORD_VERSION='5.2' bundle update
|
25
|
+
ACTIVERECORD_VERSION='5.2' bundle exec rake test
|
26
|
+
fi
|
27
|
+
|
28
|
+
if [[ $ruby_v =~ '2.5.' ]] || [[ $ruby_v =~ '2.6.' ]] || [[ $ruby_v =~ '2.7.' ]]; then
|
29
|
+
ACTIVERECORD_VERSION='6.0' bundle update
|
30
|
+
ACTIVERECORD_VERSION='6.0' bundle exec rake test
|
31
|
+
fi
|
data/.travis.yml
CHANGED
@@ -1,6 +1,30 @@
|
|
1
1
|
---
|
2
2
|
language: ruby
|
3
|
-
|
3
|
+
|
4
|
+
sudo: false
|
5
|
+
|
4
6
|
rvm:
|
5
|
-
|
6
|
-
|
7
|
+
- 2.2.0
|
8
|
+
- 2.3.0
|
9
|
+
- 2.4.0
|
10
|
+
- 2.5.0
|
11
|
+
- 2.6.0
|
12
|
+
- 2.7.0
|
13
|
+
|
14
|
+
cache: bundler
|
15
|
+
|
16
|
+
before_install:
|
17
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
18
|
+
- gem install bundler -v '< 2'
|
19
|
+
|
20
|
+
install: bundle install --jobs=3 --retry=3
|
21
|
+
|
22
|
+
before_script:
|
23
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
24
|
+
- chmod +x ./cc-test-reporter
|
25
|
+
- "./cc-test-reporter before-build"
|
26
|
+
|
27
|
+
script: "./.travis.sh"
|
28
|
+
|
29
|
+
after_success:
|
30
|
+
- "./cc-test-reporter after-build -t simplecov"
|
data/Gemfile
CHANGED
@@ -3,12 +3,36 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in u-observers.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
activerecord_version = ENV.fetch('ACTIVERECORD_VERSION', '6.1')
|
7
|
+
|
8
|
+
activerecord = case activerecord_version
|
9
|
+
when '3.2' then '3.2.22'
|
10
|
+
when '4.0' then '4.0.13'
|
11
|
+
when '4.1' then '4.1.16'
|
12
|
+
when '4.2' then '4.2.11'
|
13
|
+
when '5.0' then '5.0.7'
|
14
|
+
when '5.1' then '5.1.7'
|
15
|
+
when '5.2' then '5.2.3'
|
16
|
+
when '6.0' then '6.0.3'
|
17
|
+
end
|
18
|
+
|
19
|
+
simplecov_version =
|
20
|
+
case RUBY_VERSION
|
21
|
+
when /\A2.[23]/ then '~> 0.17.1'
|
22
|
+
when /\A2.4/ then '~> 0.18.5'
|
23
|
+
else '~> 0.19'
|
24
|
+
end
|
25
|
+
|
26
|
+
sqlite3 =
|
27
|
+
case activerecord
|
28
|
+
when /\A6\.0/, nil then '~> 1.4.0'
|
29
|
+
else '~> 1.3.0'
|
30
|
+
end
|
8
31
|
|
9
32
|
group :test do
|
10
|
-
gem '
|
11
|
-
gem '
|
33
|
+
gem 'minitest', activerecord_version < '4.1' ? '~> 4.2' : '~> 5.0'
|
34
|
+
gem 'simplecov', simplecov_version, require: false
|
12
35
|
|
13
|
-
gem '
|
36
|
+
gem 'sqlite3', sqlite3
|
37
|
+
gem 'activerecord', activerecord, require: 'active_record'
|
14
38
|
end
|
data/lib/micro/observers.rb
CHANGED
@@ -3,20 +3,19 @@ require 'micro/observers/version'
|
|
3
3
|
module Micro
|
4
4
|
module Observers
|
5
5
|
require 'micro/observers/utils'
|
6
|
-
require 'micro/observers/
|
6
|
+
require 'micro/observers/events'
|
7
7
|
require 'micro/observers/manager'
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
-
def notify_observers!(
|
11
|
-
proc
|
10
|
+
def notify_observers!(events)
|
11
|
+
proc do |object|
|
12
|
+
object.observers.subject_changed!
|
13
|
+
object.observers.send(:notify!, events)
|
14
|
+
end
|
12
15
|
end
|
13
16
|
|
14
17
|
def notify_observers(*events)
|
15
|
-
notify_observers!(
|
16
|
-
end
|
17
|
-
|
18
|
-
def call_observers(options = Utils::EMPTY_HASH)
|
19
|
-
notify_observers!(with: EventsOrActions.fetch_actions(options))
|
18
|
+
notify_observers!(Events[events])
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Micro
|
4
4
|
module Observers
|
5
5
|
|
6
|
-
module
|
6
|
+
module Events
|
7
7
|
DEFAULTS = [:call]
|
8
8
|
|
9
9
|
def self.[](value)
|
@@ -12,12 +12,6 @@ module Micro
|
|
12
12
|
values.empty? ? DEFAULTS : values
|
13
13
|
end
|
14
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
15
|
private_constant :DEFAULTS
|
22
16
|
end
|
23
17
|
|
@@ -4,31 +4,81 @@ module Micro
|
|
4
4
|
module Observers
|
5
5
|
|
6
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
|
+
|
7
14
|
EqualTo = -> (observer) do
|
8
|
-
->
|
15
|
+
-> subscriber do
|
16
|
+
handler = subscriber[0] == :observer ? subscriber[1] : subscriber[2][0]
|
17
|
+
handler == observer
|
18
|
+
end
|
9
19
|
end
|
10
20
|
|
11
21
|
def self.for(subject)
|
12
22
|
new(subject)
|
13
23
|
end
|
14
24
|
|
15
|
-
def initialize(subject,
|
25
|
+
def initialize(subject, subscribers: nil)
|
16
26
|
@subject = subject
|
17
27
|
|
18
|
-
@
|
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)
|
19
63
|
end
|
20
64
|
|
21
65
|
def included?(observer)
|
22
|
-
@
|
66
|
+
@subscribers.any?(&EqualTo[observer])
|
23
67
|
end
|
24
68
|
|
25
69
|
def attach(*args)
|
26
70
|
options = args.last.is_a?(Hash) ? args.pop : Utils::EMPTY_HASH
|
27
71
|
|
28
72
|
Utils.compact_array(args).each do |observer|
|
29
|
-
|
30
|
-
|
31
|
-
|
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])
|
32
82
|
end
|
33
83
|
|
34
84
|
self
|
@@ -39,62 +89,66 @@ module Micro
|
|
39
89
|
|
40
90
|
return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
|
41
91
|
|
42
|
-
@
|
43
|
-
end
|
44
|
-
|
45
|
-
def detach(*args)
|
46
|
-
Utils.compact_array(args).each do |observer|
|
47
|
-
@list.delete_if(&EqualTo[observer])
|
48
|
-
end
|
92
|
+
@subscribers << [:callable, event, [callable, with]] unless included?(callable)
|
49
93
|
|
50
94
|
self
|
51
95
|
end
|
52
96
|
|
53
97
|
def notify(*events)
|
54
|
-
|
55
|
-
|
56
|
-
self
|
98
|
+
notify!(Events[events])
|
57
99
|
end
|
58
100
|
|
59
|
-
def call(
|
60
|
-
broadcast(
|
101
|
+
def call(*events)
|
102
|
+
broadcast(Events[events])
|
61
103
|
|
62
104
|
self
|
63
105
|
end
|
64
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
|
+
|
65
119
|
private
|
66
120
|
|
67
|
-
def broadcast(
|
68
|
-
|
69
|
-
@
|
70
|
-
call!(observer, strategy, data, with: evt_or_act)
|
71
|
-
end
|
121
|
+
def broadcast(events)
|
122
|
+
events.each do |event|
|
123
|
+
@subscribers.each { |subscriber| call!(subscriber, event) }
|
72
124
|
end
|
73
125
|
end
|
74
126
|
|
75
|
-
def call!(
|
76
|
-
|
127
|
+
def call!(subscriber, event)
|
128
|
+
strategy, observer, context = subscriber
|
129
|
+
|
130
|
+
return call_observer(observer, event, context) if strategy == :observer
|
77
131
|
|
78
|
-
return
|
132
|
+
return call_callable(context) if strategy == :callable && observer == event
|
79
133
|
end
|
80
134
|
|
81
|
-
def call_callable(
|
82
|
-
callable,
|
135
|
+
def call_callable(context)
|
136
|
+
callable, with = context[0], context[1]
|
83
137
|
|
84
|
-
|
138
|
+
arg = with.is_a?(Proc) ? with.call(@subject) : (with || @subject)
|
85
139
|
|
86
|
-
callable.call(
|
140
|
+
callable.call(arg)
|
87
141
|
end
|
88
142
|
|
89
|
-
def call_observer(observer, method_name,
|
143
|
+
def call_observer(observer, method_name, context)
|
90
144
|
return unless observer.respond_to?(method_name)
|
91
145
|
|
92
|
-
handler = observer.method(method_name)
|
146
|
+
handler = observer.is_a?(Proc) ? observer : observer.method(method_name)
|
93
147
|
|
94
|
-
handler.arity == 1 ? handler.call(@subject) : handler.call(@subject,
|
148
|
+
handler.arity == 1 ? handler.call(@subject) : handler.call(@subject, context)
|
95
149
|
end
|
96
150
|
|
97
|
-
private_constant :EqualTo
|
151
|
+
private_constant :MapObserver, :MapSubscribers, :EqualTo, :INVALID_BOOLEAN_ERROR
|
98
152
|
end
|
99
153
|
|
100
154
|
end
|
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: 0.
|
4
|
+
version: 0.9.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-
|
12
|
-
dependencies:
|
11
|
+
date: 2020-09-29 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,6 +46,8 @@ 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
|
@@ -27,11 +57,12 @@ files:
|
|
27
57
|
- bin/console
|
28
58
|
- bin/setup
|
29
59
|
- lib/micro/observers.rb
|
30
|
-
- lib/micro/observers/
|
60
|
+
- lib/micro/observers/events.rb
|
31
61
|
- lib/micro/observers/manager.rb
|
32
62
|
- lib/micro/observers/utils.rb
|
33
63
|
- lib/micro/observers/version.rb
|
34
64
|
- lib/u-observers.rb
|
65
|
+
- test.sh
|
35
66
|
- u-observers.gemspec
|
36
67
|
homepage: https://github.com/serradura/u-observers
|
37
68
|
licenses:
|