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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b67e1c24feb02de944be44aacb6bba935d9dc1dfac6c53de2ba95dcffbd93ee
4
- data.tar.gz: aead6fb10e5ada014fed52383f031d140363cbf4f7ef9b21f5ee7c9e9f54c792
3
+ metadata.gz: bfa6d5600e86dd6086b2d6ebd4cd1b015ebe3331bd4cd61be90f2d96a1abf601
4
+ data.tar.gz: b483b0721def3d41085260343edf1041905b376264354b6ffbd07cbb5386725f
5
5
  SHA512:
6
- metadata.gz: a77a575068bdee0f9312e25e5623a4031bb1369dee11df5d039121e84cf7cab418b8b37c3c93ff46265528304b64f80fc1be61ca42e78d9436e9e779c390b2bd
7
- data.tar.gz: f90045332776294e6bc3bf6e70c34b0f24f40551f32a4d9be029ecd5fdfcad4b68d33b6f5c81802e9e257dedf9d1e16395bda434ee2dd48f38f9e291099a8a4e
6
+ metadata.gz: '06318ee21d6b9e842fa7ad752f9ccb137286667211fc43bed267d5593506d239fe946f03159236e00e0549c45e71d5de1f36cb1213ce698c3ad3b8edaf4c5abf'
7
+ data.tar.gz: 91aac7ae90a990f5575838bf13ddfd174066257a2b82620e82fc8af963519d9c85526aa742005b3d7c1cc78c1dba67d557f62ab233d34b30b70b8c69a945a929
@@ -0,0 +1 @@
1
+ ruby 2.6.5
@@ -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
@@ -1,6 +1,30 @@
1
1
  ---
2
2
  language: ruby
3
- cache: bundler
3
+
4
+ sudo: false
5
+
4
6
  rvm:
5
- - 2.6.5
6
- before_install: gem install bundler -v 2.1.4
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
- gem 'rake', '~> 12.0'
7
- gem 'minitest', '~> 5.0'
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 'activerecord', require: 'active_record'
11
- gem 'sqlite3'
33
+ gem 'minitest', activerecord_version < '4.1' ? '~> 4.2' : '~> 5.0'
34
+ gem 'simplecov', simplecov_version, require: false
12
35
 
13
- gem 'simplecov', '~> 0.19', require: false
36
+ gem 'sqlite3', sqlite3
37
+ gem 'activerecord', activerecord, require: 'active_record'
14
38
  end
@@ -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/events_or_actions'
6
+ require 'micro/observers/events'
7
7
  require 'micro/observers/manager'
8
8
 
9
9
  module ClassMethods
10
- def notify_observers!(with:)
11
- proc { |object| with.each { |evt_or_act| object.observers.notify(evt_or_act) } }
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!(with: EventsOrActions[events])
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 EventsOrActions
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
- -> item { item[0] == :observer && item[1] == observer }
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, list = nil)
25
+ def initialize(subject, subscribers: nil)
16
26
  @subject = subject
17
27
 
18
- @list = Utils.compact_array(list.kind_of?(Array) ? list : [])
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
- @list.any?(&EqualTo[observer])
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
- if options[:allow_duplication] || !included?(observer)
30
- @list << [:observer, observer, options[:data]]
31
- end
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
- @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
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
- broadcast(EventsOrActions[events])
55
-
56
- self
98
+ notify!(Events[events])
57
99
  end
58
100
 
59
- def call(options = Utils::EMPTY_HASH)
60
- broadcast(EventsOrActions.fetch_actions(options))
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(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
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!(observer, strategy, data, with:)
76
- return call_callable(data) if strategy == :callable && observer == with
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 call_observer(observer, with, data) if strategy == :observer
132
+ return call_callable(context) if strategy == :callable && observer == event
79
133
  end
80
134
 
81
- def call_callable(data)
82
- callable, arg = data[0], data[1]
135
+ def call_callable(context)
136
+ callable, with = context[0], context[1]
83
137
 
84
- callable_arg = arg.is_a?(Proc) ? arg.call(@subject) : (arg || @subject)
138
+ arg = with.is_a?(Proc) ? with.call(@subject) : (with || @subject)
85
139
 
86
- callable.call(callable_arg)
140
+ callable.call(arg)
87
141
  end
88
142
 
89
- def call_observer(observer, method_name, data)
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, data)
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
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Observers
3
- VERSION = '0.8.0'
3
+ VERSION = '0.9.0'
4
4
  end
5
5
  end
data/test.sh ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/bash
2
+
3
+ bundle
4
+
5
+ rm Gemfile.lock
6
+
7
+ source $(dirname $0)/.travis.sh
8
+
9
+ rm Gemfile.lock
10
+
11
+ bundle
@@ -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.8.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-28 00:00:00.000000000 Z
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/events_or_actions.rb
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: