signal 1.0.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/examples/listener.rb CHANGED
@@ -1,5 +1,7 @@
1
- $:.unshift File.expand_path('../../lib', __FILE__)
2
- require 'signal'
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "signal"
3
5
 
4
6
  class Status
5
7
  include Signal
@@ -11,7 +13,7 @@ end
11
13
 
12
14
  class MyListener
13
15
  def before_ready
14
- puts 'Before the ready event!'
16
+ puts "Before the ready event!"
15
17
  end
16
18
 
17
19
  def on_ready
@@ -19,7 +21,7 @@ class MyListener
19
21
  end
20
22
 
21
23
  def after_ready
22
- puts 'After the ready event!'
24
+ puts "After the ready event!"
23
25
  end
24
26
  end
25
27
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Signal
2
4
  def self.active_record
3
5
  Extensions::ActiveRecord
@@ -17,16 +19,16 @@ module Signal
17
19
  end
18
20
  end
19
21
 
20
- private
21
- def around_create_signal
22
+ private def around_create_signal
22
23
  emit_signal(:before, :create, self)
23
24
  yield
24
25
  return unless persisted?
26
+
25
27
  emit_signal(:on, :create, self)
26
28
  emit_signal(:after, :create, self)
27
29
  end
28
30
 
29
- def around_save_signal
31
+ private def around_save_signal
30
32
  if new_record?
31
33
  yield
32
34
  return
@@ -38,18 +40,18 @@ module Signal
38
40
  emit_signal(:after, :update, self)
39
41
  end
40
42
 
41
- def around_destroy_signal
43
+ private def around_destroy_signal
42
44
  emit_signal(:before, :remove, self)
43
45
  yield
44
46
  emit_signal(:on, :remove, self)
45
47
  emit_signal(:after, :remove, self)
46
48
  end
47
49
 
48
- def before_validation_signal
50
+ private def before_validation_signal
49
51
  emit_signal(:before, :validation, self)
50
52
  end
51
53
 
52
- def after_validation_signal
54
+ private def after_validation_signal
53
55
  emit_signal(:on, :validation, self) if errors.any?
54
56
  emit_signal(:after, :validation, self)
55
57
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Signal
2
4
  def self.call
3
5
  Extensions::Call
@@ -11,9 +13,9 @@ module Signal
11
13
  end
12
14
 
13
15
  module ClassMethods
14
- def call(*args, &block)
15
- new(*args).tap do |instance|
16
- yield(instance)
16
+ def call(*args, **kwargs)
17
+ new(*args, **kwargs).tap do |instance|
18
+ yield(instance) if block_given?
17
19
  instance.call
18
20
  end
19
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Signal
2
4
  class Listener
3
5
  def initialize(context, type, event, &block)
@@ -10,6 +12,7 @@ module Signal
10
12
 
11
13
  def method_missing(method_name, *args)
12
14
  return super unless respond_to_missing?(method_name, false)
15
+
13
16
  @block.call(*args)
14
17
  end
15
18
 
@@ -17,7 +20,7 @@ module Signal
17
20
  "<#{self.class} event: #{@event_method}>"
18
21
  end
19
22
 
20
- def respond_to_missing?(method_name, include_private)
23
+ def respond_to_missing?(method_name, _include_private)
21
24
  method_name == @event_method
22
25
  end
23
26
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Signal
4
+ class Mock
5
+ def calls
6
+ @calls ||= []
7
+ end
8
+
9
+ def method_missing(name, *args)
10
+ return super unless respond_to_missing?(name)
11
+
12
+ calls << {event: name.to_s.gsub(/^on_/, "").to_sym, args: args}
13
+ end
14
+
15
+ def respond_to_missing?(name, _include_all = false)
16
+ name =~ /^on_/
17
+ end
18
+
19
+ def received?(event, options = {})
20
+ received_event?(event, options[:times] || -1) &&
21
+ received_with?(event, options[:with])
22
+ end
23
+
24
+ def to_proc
25
+ proc {|action| action.add_listener(self) }
26
+ end
27
+
28
+ def on(event)
29
+ proc {|*args| calls << {event: event, args: args} }
30
+ end
31
+
32
+ private def received_event?(event, count)
33
+ received_calls = calls.count {|call| call[:event] == event }
34
+
35
+ return received_calls.nonzero? if count == -1
36
+
37
+ received_calls == count
38
+ end
39
+
40
+ private def received_with?(event, args)
41
+ return true unless args
42
+
43
+ calls.any? do |call|
44
+ next unless call[:event] == event
45
+ next args.call(call[:args]) if args.is_a?(Proc)
46
+
47
+ args == call[:args]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Signal
2
- VERSION = '1.0.0'
4
+ VERSION = "1.3.1"
3
5
  end
data/lib/signal.rb CHANGED
@@ -1,7 +1,9 @@
1
- require 'signal/version'
2
- require 'signal/listener'
3
- require 'signal/extensions/active_record'
4
- require 'signal/extensions/call'
1
+ # frozen_string_literal: true
2
+
3
+ require "signal/version"
4
+ require "signal/listener"
5
+ require "signal/extensions/active_record"
6
+ require "signal/extensions/call"
5
7
 
6
8
  module Signal
7
9
  def on(event, &block)
@@ -35,11 +37,13 @@ module Signal
35
37
  @listeners ||= []
36
38
  end
37
39
 
38
- private
39
- def emit_signal(type, event, *args)
40
+ private def emit_signal(type, event, *args)
40
41
  listeners.each do |listener|
41
42
  method_name = "#{type}_#{event}"
42
- listener.send(method_name, *args) if listener.respond_to?(method_name, true)
43
+
44
+ if listener.respond_to?(method_name, true)
45
+ listener.send(method_name, *args)
46
+ end
43
47
  end
44
48
  end
45
49
  end
data/signal.gemspec CHANGED
@@ -1,21 +1,43 @@
1
- require './lib/signal/version'
1
+ # frozen_string_literal: true
2
2
 
3
- Gem::Specification.new do |gem|
4
- gem.name = 'signal'
5
- gem.version = Signal::VERSION
6
- gem.authors = ['Nando Vieira']
7
- gem.email = ['fnando.vieira@gmail.com']
8
- gem.description = 'A simple observer implementation for POROs (Plain Old Ruby Object) and ActiveRecord objects.'
9
- gem.summary = gem.description
10
- gem.homepage = 'http://github.com/fnando/signal'
3
+ require "./lib/signal/version"
11
4
 
12
- gem.files = `git ls-files`.split($/)
13
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
- gem.require_paths = ['lib']
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "signal"
7
+ spec.version = Signal::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["fnando.vieira@gmail.com"]
10
+ spec.description = %w[
11
+ A simple observer implementation for POROs (Plain Old Ruby Object) and
12
+ ActiveRecord objects.
13
+ ].join(" ")
14
+ spec.summary = spec.description
15
+ spec.homepage = "http://github.com/fnando/signal"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
16
17
 
17
- gem.add_development_dependency 'activerecord'
18
- gem.add_development_dependency 'sqlite3'
19
- gem.add_development_dependency 'rspec'
20
- gem.add_development_dependency 'codeclimate-test-reporter'
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.executables = spec.files.grep(%r{^bin/}).map {|f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.post_install_message =
27
+ "\n====================================================================\n" \
28
+ "`signal` has been deprecated in favor of `voltage`.\n" \
29
+ "To install it, use `gem install voltage` or add it to your gemfile.\n" \
30
+ "For details, please visit https://github.com/fnando/signal/issues/6" \
31
+ "\n====================================================================\n\n"
32
+
33
+ spec.add_dependency "voltage"
34
+
35
+ spec.add_development_dependency "activerecord"
36
+ spec.add_development_dependency "minitest-utils"
37
+ spec.add_development_dependency "pry-meta"
38
+ spec.add_development_dependency "rake"
39
+ spec.add_development_dependency "rubocop"
40
+ spec.add_development_dependency "rubocop-fnando"
41
+ spec.add_development_dependency "simplecov"
42
+ spec.add_development_dependency "sqlite3"
21
43
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: voltage
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activerecord
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,7 +39,21 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: sqlite3
42
+ name: minitest-utils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-meta
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
59
  - - ">="
@@ -39,7 +67,7 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
- name: rspec
70
+ name: rake
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - ">="
@@ -53,7 +81,49 @@ dependencies:
53
81
  - !ruby/object:Gem::Version
54
82
  version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
- name: codeclimate-test-reporter
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-fnando
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
57
127
  requirement: !ruby/object:Gem::Requirement
58
128
  requirements:
59
129
  - - ">="
@@ -74,10 +144,15 @@ executables: []
74
144
  extensions: []
75
145
  extra_rdoc_files: []
76
146
  files:
147
+ - ".github/CODEOWNERS"
148
+ - ".github/FUNDING.yml"
149
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
150
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
151
+ - ".github/PULL_REQUEST_TEMPLATE.md"
152
+ - ".github/dependabot.yml"
153
+ - ".github/workflows/ruby-tests.yml"
77
154
  - ".gitignore"
78
- - ".rspec"
79
- - ".travis.yml"
80
- - CHANGELOG.md
155
+ - ".rubocop.yml"
81
156
  - Gemfile
82
157
  - LICENSE.txt
83
158
  - README.md
@@ -86,29 +161,27 @@ files:
86
161
  - examples/arguments.rb
87
162
  - examples/block_context.rb
88
163
  - examples/blocks.rb
164
+ - examples/call.rb
89
165
  - examples/chain.rb
90
166
  - examples/listener.rb
91
167
  - lib/signal.rb
92
168
  - lib/signal/extensions/active_record.rb
93
169
  - lib/signal/extensions/call.rb
94
170
  - lib/signal/listener.rb
171
+ - lib/signal/mock.rb
95
172
  - lib/signal/version.rb
96
173
  - signal.gemspec
97
- - spec/signal/activerecord_spec.rb
98
- - spec/signal/call_spec.rb
99
- - spec/signal/issues/2_spec.rb
100
- - spec/signal/listener_spec.rb
101
- - spec/signal/signal_spec.rb
102
- - spec/spec_helper.rb
103
- - spec/support/callable.rb
104
- - spec/support/emitter.rb
105
- - spec/support/observable.rb
106
- - spec/support/observable_with_call.rb
107
- - spec/support/user.rb
108
174
  homepage: http://github.com/fnando/signal
109
175
  licenses: []
110
176
  metadata: {}
111
- post_install_message:
177
+ post_install_message: |2+
178
+
179
+ ====================================================================
180
+ `signal` has been deprecated in favor of `voltage`.
181
+ To install it, use `gem install voltage` or add it to your gemfile.
182
+ For details, please visit https://github.com/fnando/signal/issues/6
183
+ ====================================================================
184
+
112
185
  rdoc_options: []
113
186
  require_paths:
114
187
  - lib
@@ -116,28 +189,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
189
  requirements:
117
190
  - - ">="
118
191
  - !ruby/object:Gem::Version
119
- version: '0'
192
+ version: 2.7.0
120
193
  required_rubygems_version: !ruby/object:Gem::Requirement
121
194
  requirements:
122
195
  - - ">="
123
196
  - !ruby/object:Gem::Version
124
197
  version: '0'
125
198
  requirements: []
126
- rubyforge_project:
127
- rubygems_version: 2.4.5.1
128
- signing_key:
199
+ rubygems_version: 3.2.32
200
+ signing_key:
129
201
  specification_version: 4
130
202
  summary: A simple observer implementation for POROs (Plain Old Ruby Object) and ActiveRecord
131
203
  objects.
132
- test_files:
133
- - spec/signal/activerecord_spec.rb
134
- - spec/signal/call_spec.rb
135
- - spec/signal/issues/2_spec.rb
136
- - spec/signal/listener_spec.rb
137
- - spec/signal/signal_spec.rb
138
- - spec/spec_helper.rb
139
- - spec/support/callable.rb
140
- - spec/support/emitter.rb
141
- - spec/support/observable.rb
142
- - spec/support/observable_with_call.rb
143
- - spec/support/user.rb
204
+ test_files: []
205
+ ...
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/.travis.yml DELETED
@@ -1,11 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- sudo: false
4
- script: bundle exec rspec
5
- rvm:
6
- - '2.2'
7
- - '2.1'
8
- addons:
9
- code_climate:
10
- repo_token:
11
- secure: "ASogQTLS/xYYczRtFwuo8DIkNdOl16mez4GHOw4RtdfTQ4BMROxE/CUemdPulMW1b/p7jsp5ghOtH2EJ4jN203s83HN4QjGC2wXhigkk6LzvxqjkmHxIWxJi2JS2sLq8nzOCcD0geO1pcbBFyuAzAdlwWa2g1WDP7O0L0fNjCtM="
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- # Signal
2
-
3
- ### v1.0.0
4
-
5
- - Use `include Signal.active_record` instead of `include Signal::ActiveRecord`. This fixes problems with [constant lookup](https://github.com/fnando/signal/issues/2).
@@ -1,132 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Signal::Extensions::ActiveRecord do
4
- let(:callable) { Callable.new }
5
- let(:user) { User.new(:username => 'johndoe') }
6
-
7
- context 'create event' do
8
- it 'triggers before event' do
9
- user.before(:create, &callable)
10
- expect(callable).to receive(:called).with(user)
11
- user.save!
12
- end
13
-
14
- it 'triggers event' do
15
- user.on(:create, &callable)
16
- expect(callable).to receive(:called).with(user)
17
- user.save!
18
- end
19
-
20
- it 'triggers after event' do
21
- user.after(:create, &callable)
22
- expect(callable).to receive(:called).with(user)
23
- user.save!
24
- end
25
-
26
- it 'does not trigger on/after events when record is invalid' do
27
- user = User.new
28
-
29
- on_callable = Callable.new
30
- after_callable = Callable.new
31
-
32
- user
33
- .on(:create, &on_callable)
34
- .after(:create, &after_callable)
35
-
36
- expect(on_callable).not_to receive(:called)
37
- expect(after_callable).not_to receive(:called)
38
-
39
- user.save
40
- end
41
- end
42
-
43
- context 'validation event' do
44
- it 'triggers before event' do
45
- user.before(:validation, &callable)
46
- expect(callable).to receive(:called).with(user)
47
- user.save!
48
- end
49
-
50
- it 'triggers after event' do
51
- user.after(:validation, &callable)
52
- expect(callable).to receive(:called).with(user)
53
- user.save!
54
- end
55
-
56
- it 'triggers validation event when record is invalid' do
57
- user.username = nil
58
- user.on(:validation, &callable)
59
- expect(callable).to receive(:called).with(user)
60
- user.save
61
- end
62
-
63
- it 'skips validation event when record is valid' do
64
- user.on(:validation, &callable)
65
- expect(callable).not_to receive(:called)
66
- user.save!
67
- end
68
- end
69
-
70
- context 'update event' do
71
- let(:user) { User.create!(:username => 'johndoe') }
72
-
73
- it 'triggers before event' do
74
- user.before(:update, &callable)
75
- expect(callable).to receive(:called).with(user)
76
- user.update_attributes!(:username => 'johnd')
77
- end
78
-
79
- it 'triggers on event' do
80
- user.on(:update, &callable)
81
- expect(callable).to receive(:called).with(user)
82
- user.update_attributes!(:username => 'johnd')
83
- end
84
-
85
- it 'triggers after event' do
86
- user.after(:update, &callable)
87
- expect(callable).to receive(:called).with(user)
88
- user.update_attributes!(:username => 'johnd')
89
- end
90
-
91
- it 'does not trigger on/after events when record is invalid' do
92
- user.username = nil
93
-
94
- on_callable = Callable.new
95
- after_callable = Callable.new
96
-
97
- user
98
- .on(:update, &on_callable)
99
- .after(:update, &after_callable)
100
-
101
- expect(on_callable).not_to receive(:called)
102
- expect(after_callable).not_to receive(:called)
103
-
104
- user.save
105
- end
106
- end
107
-
108
- context 'remove event' do
109
- let(:user) { User.create!(:username => 'johndoe') }
110
-
111
- it 'triggers before event' do
112
- user.before(:remove, &callable)
113
- expect(callable).to receive(:called).with(user)
114
-
115
- user.destroy
116
- end
117
-
118
- it 'triggers on event' do
119
- user.on(:remove, &callable)
120
- expect(callable).to receive(:called).with(user)
121
-
122
- user.destroy
123
- end
124
-
125
- it 'triggers after event' do
126
- user.after(:remove, &callable)
127
- expect(callable).to receive(:called).with(user)
128
-
129
- user.destroy
130
- end
131
- end
132
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Signal::Extensions::Call do
4
- let(:callable) { Callable.new }
5
-
6
- it 'initializes observable with arguments' do
7
- observable = ObservableWithCall.call(1, 2, 3) {}
8
- expect(observable.args).to eq([1, 2, 3])
9
- end
10
-
11
- it 'triggers event' do
12
- expect(callable).to receive(:called).with([1, 2, 3])
13
-
14
- ObservableWithCall.call(1, 2, 3) do |o|
15
- o.on(:args, &callable)
16
- end
17
- end
18
- end
@@ -1,13 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Constant lookup', 'https://github.com/fnando/signal/issues/2' do
4
- let(:callable) { Callable.new }
5
-
6
- it 'triggers event' do
7
- emitter = Emitter.new
8
- emitter.on(:success, &callable)
9
- expect(callable).to receive(:called).with(no_args)
10
-
11
- emitter.call
12
- end
13
- end
@@ -1,8 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Signal::Listener do
4
- it 'sets string representation' do
5
- listener = Signal::Listener.new(self, :on, :ready, &->{})
6
- expect(listener.to_s).to eq('<Signal::Listener event: on_ready>')
7
- end
8
- end