signal 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Rakefile +10 -1
- data/examples/activerecord_model.rb +8 -8
- data/examples/arguments.rb +2 -2
- data/examples/block_context.rb +3 -3
- data/examples/blocks.rb +4 -4
- data/examples/call.rb +20 -0
- data/examples/chain.rb +11 -11
- data/examples/listener.rb +4 -4
- data/lib/signal.rb +4 -4
- data/lib/signal/extensions/call.rb +2 -2
- data/lib/signal/version.rb +1 -1
- data/signal.gemspec +13 -12
- data/test/signal/activerecord_test.rb +169 -0
- data/test/signal/call_test.rb +19 -0
- data/test/signal/issues_test.rb +15 -0
- data/test/signal/listener_test.rb +8 -0
- data/test/signal/signal_test.rb +146 -0
- data/test/support/callable.rb +11 -0
- data/{spec → test}/support/emitter.rb +0 -0
- data/{spec → test}/support/observable.rb +0 -0
- data/{spec → test}/support/observable_with_call.rb +0 -0
- data/{spec → test}/support/user.rb +0 -0
- data/test/test_helper.rb +28 -0
- metadata +41 -27
- data/.rspec +0 -1
- data/spec/signal/activerecord_spec.rb +0 -132
- data/spec/signal/call_spec.rb +0 -18
- data/spec/signal/issues/2_spec.rb +0 -13
- data/spec/signal/listener_spec.rb +0 -8
- data/spec/signal/signal_spec.rb +0 -123
- data/spec/spec_helper.rb +0 -25
- data/spec/support/callable.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03e8ba417f4a98d92feeef3793b692fb4adec8eb
|
4
|
+
data.tar.gz: c0d5bdfa9dc3e51ee050009b8d4164ca0622c91f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1f56b758b92d0786a7808a75f7765fe8c612c8d26183627e5401a7eebfb02cbd78bb597c7249f42edd532e73283d3ca39fd8fbf626e010489d141aa72350f5a
|
7
|
+
data.tar.gz: f2219aa9e7d6551deceb844a3b302313e254126495cd369561e3f2f194a79acd5aadd487e8003143e35038b93783e94835d12fc14334baf865dc98a1806d7c9f
|
data/.travis.yml
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
language: ruby
|
2
2
|
cache: bundler
|
3
3
|
sudo: false
|
4
|
-
script: bundle exec
|
4
|
+
script: bundle exec rake
|
5
5
|
rvm:
|
6
|
+
- '2.3.0'
|
6
7
|
- '2.2'
|
7
8
|
- '2.1'
|
8
9
|
addons:
|
9
10
|
code_climate:
|
10
11
|
repo_token:
|
11
12
|
secure: "ASogQTLS/xYYczRtFwuo8DIkNdOl16mez4GHOw4RtdfTQ4BMROxE/CUemdPulMW1b/p7jsp5ghOtH2EJ4jN203s83HN4QjGC2wXhigkk6LzvxqjkmHxIWxJi2JS2sLq8nzOCcD0geO1pcbBFyuAzAdlwWa2g1WDP7O0L0fNjCtM="
|
13
|
+
notifications:
|
14
|
+
email: false
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
$:.unshift File.expand_path(
|
2
|
-
require
|
3
|
-
require
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
|
+
require "active_record"
|
4
4
|
|
5
5
|
ActiveRecord::Base.establish_connection({
|
6
|
-
:adapter =>
|
7
|
-
:database =>
|
6
|
+
:adapter => "sqlite3",
|
7
|
+
:database => ":memory:"
|
8
8
|
})
|
9
9
|
|
10
10
|
ActiveRecord::Schema.define(:version => 0) do
|
@@ -20,7 +20,7 @@ class Thing < ActiveRecord::Base
|
|
20
20
|
validates_presence_of :name
|
21
21
|
end
|
22
22
|
|
23
|
-
thing = Thing.new(:name =>
|
23
|
+
thing = Thing.new(:name => "Stuff")
|
24
24
|
thing.on(:create) {|model| puts model.updated_at, model.name }
|
25
25
|
thing.on(:update) {|model| puts model.updated_at, model.name }
|
26
26
|
thing.on(:remove) {|model| puts model.destroyed? }
|
@@ -30,12 +30,12 @@ thing.save!
|
|
30
30
|
#=> 2013-01-26 10:32:39 -0200
|
31
31
|
#=> Stuff
|
32
32
|
|
33
|
-
thing.update_attributes(:name =>
|
33
|
+
thing.update_attributes(:name => "Updated stuff")
|
34
34
|
#=> 2013-01-26 10:33:11 -0200
|
35
35
|
#=> Updated stuff
|
36
36
|
|
37
37
|
thing.update_attributes(:name => nil)
|
38
|
-
#=> [
|
38
|
+
#=> ["Name can"t be blank"]
|
39
39
|
|
40
40
|
thing.destroy
|
41
41
|
#=> true
|
data/examples/arguments.rb
CHANGED
data/examples/block_context.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$:.unshift File.expand_path(
|
2
|
-
require
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
3
|
|
4
4
|
class Contact
|
5
5
|
include Signal
|
@@ -15,6 +15,6 @@ class Contact
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
contact = Contact.new(
|
18
|
+
contact = Contact.new("John Doe", "john@example.org")
|
19
19
|
contact.on(:output) {|contact| puts contact.name, contact.email }
|
20
20
|
contact.output!
|
data/examples/blocks.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$:.unshift File.expand_path(
|
2
|
-
require
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
3
|
|
4
4
|
class Status
|
5
5
|
include Signal
|
@@ -10,7 +10,7 @@ class Status
|
|
10
10
|
end
|
11
11
|
|
12
12
|
status = Status.new
|
13
|
-
status.before(:ready) { puts
|
13
|
+
status.before(:ready) { puts "Before the ready event!" }
|
14
14
|
status.on(:ready) { puts "I'm ready!" }
|
15
|
-
status.after(:ready) { puts
|
15
|
+
status.after(:ready) { puts "After the ready event!" }
|
16
16
|
status.ready!
|
data/examples/call.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
|
+
|
4
|
+
class Contact
|
5
|
+
include Signal.call
|
6
|
+
|
7
|
+
attr_reader :name, :email
|
8
|
+
|
9
|
+
def initialize(name, email)
|
10
|
+
@name, @email = name, email
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
emit(:output, self)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Contact.call("John", "john@example.com") do |o|
|
19
|
+
o.on(:output) {|contact| puts contact.name }
|
20
|
+
end
|
data/examples/chain.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
$:.unshift File.expand_path(
|
2
|
-
require
|
3
|
-
require
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
|
+
require "active_record"
|
4
4
|
|
5
5
|
ActiveRecord::Base.establish_connection({
|
6
|
-
:adapter =>
|
7
|
-
:database =>
|
6
|
+
:adapter => "sqlite3",
|
7
|
+
:database => ":memory:"
|
8
8
|
})
|
9
9
|
|
10
10
|
ActiveRecord::Schema.define(:version => 0) do
|
@@ -15,7 +15,7 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class Thing < ActiveRecord::Base
|
18
|
-
include Signal
|
18
|
+
include Signal.active_record
|
19
19
|
|
20
20
|
validates_presence_of :name
|
21
21
|
end
|
@@ -31,7 +31,7 @@ class MyListener
|
|
31
31
|
end
|
32
32
|
|
33
33
|
puts "\n=== Creating valid record"
|
34
|
-
thing = Thing.new(:name =>
|
34
|
+
thing = Thing.new(:name => "Stuff")
|
35
35
|
thing.listeners << MyListener.new
|
36
36
|
thing.save
|
37
37
|
|
@@ -41,16 +41,16 @@ thing.listeners << MyListener.new
|
|
41
41
|
thing.save
|
42
42
|
|
43
43
|
puts "\n=== Updating valid record"
|
44
|
-
thing = Thing.create(:name =>
|
44
|
+
thing = Thing.create(:name => "Stuff")
|
45
45
|
thing.listeners << MyListener.new
|
46
|
-
thing.update_attributes(:name =>
|
46
|
+
thing.update_attributes(:name => "Updated stuff")
|
47
47
|
|
48
48
|
puts "\n=== Updating invalid record"
|
49
|
-
thing = Thing.create!(:name =>
|
49
|
+
thing = Thing.create!(:name => "Stuff")
|
50
50
|
thing.listeners << MyListener.new
|
51
51
|
thing.update_attributes(:name => nil)
|
52
52
|
|
53
53
|
puts "\n=== Removing record"
|
54
|
-
thing = Thing.create(:name =>
|
54
|
+
thing = Thing.create(:name => "Stuff")
|
55
55
|
thing.listeners << MyListener.new
|
56
56
|
thing.destroy
|
data/examples/listener.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$:.unshift File.expand_path(
|
2
|
-
require
|
1
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "signal"
|
3
3
|
|
4
4
|
class Status
|
5
5
|
include Signal
|
@@ -11,7 +11,7 @@ end
|
|
11
11
|
|
12
12
|
class MyListener
|
13
13
|
def before_ready
|
14
|
-
puts
|
14
|
+
puts "Before the ready event!"
|
15
15
|
end
|
16
16
|
|
17
17
|
def on_ready
|
@@ -19,7 +19,7 @@ class MyListener
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def after_ready
|
22
|
-
puts
|
22
|
+
puts "After the ready event!"
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
data/lib/signal.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "signal/version"
|
2
|
+
require "signal/listener"
|
3
|
+
require "signal/extensions/active_record"
|
4
|
+
require "signal/extensions/call"
|
5
5
|
|
6
6
|
module Signal
|
7
7
|
def on(event, &block)
|
data/lib/signal/version.rb
CHANGED
data/signal.gemspec
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
require
|
1
|
+
require "./lib/signal/version"
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
|
-
gem.name =
|
4
|
+
gem.name = "signal"
|
5
5
|
gem.version = Signal::VERSION
|
6
|
-
gem.authors = [
|
7
|
-
gem.email = [
|
8
|
-
gem.description =
|
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
9
|
gem.summary = gem.description
|
10
|
-
gem.homepage =
|
10
|
+
gem.homepage = "http://github.com/fnando/signal"
|
11
11
|
|
12
12
|
gem.files = `git ls-files`.split($/)
|
13
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map {|f| File.basename(f) }
|
14
14
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
-
gem.require_paths = [
|
15
|
+
gem.require_paths = ["lib"]
|
16
16
|
|
17
|
-
gem.add_development_dependency
|
18
|
-
gem.add_development_dependency
|
19
|
-
gem.add_development_dependency
|
20
|
-
gem.add_development_dependency
|
17
|
+
gem.add_development_dependency "activerecord"
|
18
|
+
gem.add_development_dependency "sqlite3"
|
19
|
+
gem.add_development_dependency "minitest-utils"
|
20
|
+
gem.add_development_dependency "codeclimate-test-reporter"
|
21
|
+
gem.add_development_dependency "rake"
|
21
22
|
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ARCreateEventTest < Minitest::Test
|
4
|
+
let(:user) { User.new(username: "johndoe") }
|
5
|
+
|
6
|
+
test "triggers before event" do
|
7
|
+
callable = Callable.new(:before_create)
|
8
|
+
user.before(:create, &callable)
|
9
|
+
callable.expect(:before_create, nil, [user])
|
10
|
+
user.save!
|
11
|
+
|
12
|
+
assert callable.verify
|
13
|
+
end
|
14
|
+
|
15
|
+
test "triggers event" do
|
16
|
+
callable = Callable.new(:on_create)
|
17
|
+
user.on(:create, &callable)
|
18
|
+
callable.expect(:on_create, nil, [user])
|
19
|
+
user.save!
|
20
|
+
|
21
|
+
assert callable.verify
|
22
|
+
end
|
23
|
+
|
24
|
+
test "triggers after event" do
|
25
|
+
callable = Callable.new(:after_create)
|
26
|
+
user.after(:create, &callable)
|
27
|
+
callable.expect(:after_create, nil, [user])
|
28
|
+
user.save!
|
29
|
+
|
30
|
+
assert callable.verify
|
31
|
+
end
|
32
|
+
|
33
|
+
test "does not trigger events when record is invalid" do
|
34
|
+
user = User.new
|
35
|
+
calls = []
|
36
|
+
|
37
|
+
user
|
38
|
+
.before(:create) { calls << :before }
|
39
|
+
.on(:create) { calls << :on }
|
40
|
+
.after(:create) { calls << :after }
|
41
|
+
|
42
|
+
user.save
|
43
|
+
|
44
|
+
assert_equal [], calls
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ARValidationEventTest < Minitest::Test
|
49
|
+
let(:user) { User.new(username: "johndoe") }
|
50
|
+
|
51
|
+
test "triggers before event" do
|
52
|
+
callable = Callable.new(:before_validation)
|
53
|
+
user.before(:validation, &callable)
|
54
|
+
callable.expect(:before_validation, nil, [user])
|
55
|
+
user.save!
|
56
|
+
|
57
|
+
assert callable.verify
|
58
|
+
end
|
59
|
+
|
60
|
+
test "triggers after event" do
|
61
|
+
callable = Callable.new(:after_validation)
|
62
|
+
user.after(:validation, &callable)
|
63
|
+
callable.expect(:after_validation, nil, [user])
|
64
|
+
user.save!
|
65
|
+
|
66
|
+
assert callable.verify
|
67
|
+
end
|
68
|
+
|
69
|
+
test "triggers validation event when record is invalid" do
|
70
|
+
callable = Callable.new(:on_validation)
|
71
|
+
user.username = nil
|
72
|
+
user.on(:validation, &callable)
|
73
|
+
|
74
|
+
callable.expect(:on_validation, nil, [user])
|
75
|
+
user.save
|
76
|
+
|
77
|
+
assert callable.verify
|
78
|
+
end
|
79
|
+
|
80
|
+
test "skips validation event when record is valid" do
|
81
|
+
calls = []
|
82
|
+
user.on(:validation) { calls << :on }
|
83
|
+
user.save!
|
84
|
+
|
85
|
+
assert_equal [], calls
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class ARUpdateEvent < Minitest::Test
|
90
|
+
let(:user) { User.create!(username: "johndoe") }
|
91
|
+
|
92
|
+
test "triggers before event" do
|
93
|
+
callable = Callable.new(:before_update)
|
94
|
+
user.before(:update, &callable)
|
95
|
+
|
96
|
+
callable.expect(:before_update, nil, [user])
|
97
|
+
user.update_attributes!(username: "johnd")
|
98
|
+
|
99
|
+
assert callable.verify
|
100
|
+
end
|
101
|
+
|
102
|
+
test "triggers event" do
|
103
|
+
callable = Callable.new(:on_update)
|
104
|
+
user.on(:update, &callable)
|
105
|
+
|
106
|
+
callable.expect(:on_update, nil, [user])
|
107
|
+
user.update_attributes!(username: "johnd")
|
108
|
+
|
109
|
+
assert callable.verify
|
110
|
+
end
|
111
|
+
|
112
|
+
test "triggers after event" do
|
113
|
+
callable = Callable.new(:after_update)
|
114
|
+
user.after(:update, &callable)
|
115
|
+
|
116
|
+
callable.expect(:after_update, nil, [user])
|
117
|
+
user.update_attributes!(username: "johnd")
|
118
|
+
|
119
|
+
assert callable.verify
|
120
|
+
end
|
121
|
+
|
122
|
+
test "does not trigger events when record is invalid" do
|
123
|
+
user.username = nil
|
124
|
+
calls = []
|
125
|
+
|
126
|
+
user
|
127
|
+
.before(:update) { calls << :before }
|
128
|
+
.on(:update) { calls << :on }
|
129
|
+
.after(:update) { calls << :after }
|
130
|
+
|
131
|
+
user.save
|
132
|
+
|
133
|
+
assert_equal [], calls
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class ARRemoveEvent < Minitest::Test
|
138
|
+
let(:user) { User.create!(username: "johndoe") }
|
139
|
+
|
140
|
+
test "triggers before event" do
|
141
|
+
callable = Callable.new(:before_remove)
|
142
|
+
user.before(:remove, &callable)
|
143
|
+
|
144
|
+
callable.expect(:before_remove, nil, [user])
|
145
|
+
user.destroy
|
146
|
+
|
147
|
+
assert callable.verify
|
148
|
+
end
|
149
|
+
|
150
|
+
test "triggers event" do
|
151
|
+
callable = Callable.new(:on_remove)
|
152
|
+
user.on(:remove, &callable)
|
153
|
+
|
154
|
+
callable.expect(:on_remove, nil, [user])
|
155
|
+
user.destroy
|
156
|
+
|
157
|
+
assert callable.verify
|
158
|
+
end
|
159
|
+
|
160
|
+
test "triggers after event" do
|
161
|
+
callable = Callable.new(:after_remove)
|
162
|
+
user.after(:remove, &callable)
|
163
|
+
|
164
|
+
callable.expect(:after_remove, nil, [user])
|
165
|
+
user.destroy
|
166
|
+
|
167
|
+
assert callable.verify
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SignalCallTest < Minitest::Test
|
4
|
+
test "initializes observable with arguments" do
|
5
|
+
observable = ObservableWithCall.call(1, 2, 3)
|
6
|
+
assert_equal [1, 2, 3], observable.args
|
7
|
+
end
|
8
|
+
|
9
|
+
test "triggers event" do
|
10
|
+
callable = Callable.new(:on_args)
|
11
|
+
callable.expect(:on_args, nil, [[1, 2, 3]])
|
12
|
+
|
13
|
+
ObservableWithCall.call(1, 2, 3) do |o|
|
14
|
+
o.on(:args, &callable)
|
15
|
+
end
|
16
|
+
|
17
|
+
assert callable.verify
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
# https://github.com/fnando/signal/issues/2
|
4
|
+
class ConstantLookupTest < Minitest::Test
|
5
|
+
let(:callable) { Callable.new(:on_success) }
|
6
|
+
|
7
|
+
test "triggers event" do
|
8
|
+
emitter = Emitter.new
|
9
|
+
emitter.on(:success, &callable)
|
10
|
+
callable.expect(:on_success, nil, [])
|
11
|
+
emitter.call
|
12
|
+
|
13
|
+
assert callable.verify
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AddListenerTest < Minitest::Test
|
4
|
+
let(:observable) { Observable.new }
|
5
|
+
let(:callable) { Callable.new(:on_ready) }
|
6
|
+
|
7
|
+
test "runs listener" do
|
8
|
+
callable.expect(:on_ready, nil, [])
|
9
|
+
observable.add_listener(callable)
|
10
|
+
|
11
|
+
observable.emit(:ready)
|
12
|
+
|
13
|
+
assert callable.verify
|
14
|
+
end
|
15
|
+
|
16
|
+
test "returns itself" do
|
17
|
+
assert_equal observable, observable.add_listener(callable)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class UsingBlocksTest < Minitest::Test
|
22
|
+
let(:observable) { Observable.new }
|
23
|
+
let(:callable) { Callable.new(:on_ready) }
|
24
|
+
|
25
|
+
test "triggers event" do
|
26
|
+
callable.expect(:on_ready, nil, [])
|
27
|
+
observable.on(:ready, &callable)
|
28
|
+
|
29
|
+
observable.emit(:ready)
|
30
|
+
|
31
|
+
assert callable.verify
|
32
|
+
end
|
33
|
+
|
34
|
+
test "triggers event with arguments" do
|
35
|
+
callable.expect(:on_ready, nil, [1, 2, 3])
|
36
|
+
observable.on(:ready, &callable)
|
37
|
+
|
38
|
+
observable.emit(:ready, 1, 2, 3)
|
39
|
+
|
40
|
+
assert callable.verify
|
41
|
+
end
|
42
|
+
|
43
|
+
test "triggers before event" do
|
44
|
+
callable = Callable.new(:before_ready)
|
45
|
+
callable.expect(:before_ready, nil, [])
|
46
|
+
observable.before(:ready, &callable)
|
47
|
+
|
48
|
+
observable.emit(:ready)
|
49
|
+
|
50
|
+
assert callable.verify
|
51
|
+
end
|
52
|
+
|
53
|
+
test "triggers before event with arguments" do
|
54
|
+
callable = Callable.new(:before_ready)
|
55
|
+
callable.expect(:before_ready, nil, [1, 2, 3])
|
56
|
+
observable.before(:ready, &callable)
|
57
|
+
|
58
|
+
observable.emit(:ready, 1, 2, 3)
|
59
|
+
|
60
|
+
assert callable.verify
|
61
|
+
end
|
62
|
+
|
63
|
+
test "triggers after event" do
|
64
|
+
callable = Callable.new(:after_ready)
|
65
|
+
callable.expect(:after_ready, nil, [])
|
66
|
+
observable.after(:ready, &callable)
|
67
|
+
|
68
|
+
observable.emit(:ready)
|
69
|
+
|
70
|
+
assert callable.verify
|
71
|
+
end
|
72
|
+
|
73
|
+
test "triggers after event with arguments" do
|
74
|
+
callable = Callable.new(:after_ready)
|
75
|
+
callable.expect(:after_ready, nil, [1, 2, 3])
|
76
|
+
observable.after(:ready, &callable)
|
77
|
+
|
78
|
+
observable.emit(:ready, 1, 2, 3)
|
79
|
+
|
80
|
+
assert callable.verify
|
81
|
+
end
|
82
|
+
|
83
|
+
test "chains events" do
|
84
|
+
calls = []
|
85
|
+
|
86
|
+
observable
|
87
|
+
.before(:ready) { calls << :before }
|
88
|
+
.on(:ready) { calls << :on }
|
89
|
+
.after(:ready) { calls << :after }
|
90
|
+
|
91
|
+
observable.emit(:ready)
|
92
|
+
|
93
|
+
assert_equal %i[before on after], calls
|
94
|
+
end
|
95
|
+
|
96
|
+
test "keeps context" do
|
97
|
+
context = nil
|
98
|
+
callable = -> { context = self }
|
99
|
+
observable.on(:ready, &callable)
|
100
|
+
observable.emit(:ready)
|
101
|
+
|
102
|
+
assert_equal self, context
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class UsingListenersTest < Minitest::Test
|
107
|
+
let(:observable) { Observable.new }
|
108
|
+
let(:callable) { Callable.new(:on_ready) }
|
109
|
+
|
110
|
+
test "triggers event for listener" do
|
111
|
+
observable.listeners << callable
|
112
|
+
callable.expect(:after_ready, nil, [])
|
113
|
+
|
114
|
+
observable.emit(:ready)
|
115
|
+
|
116
|
+
assert callable.verify
|
117
|
+
end
|
118
|
+
|
119
|
+
test "triggers event for listener with arguments" do
|
120
|
+
observable.listeners << callable
|
121
|
+
callable.expect(:after_ready, nil, [1, 2, 3])
|
122
|
+
|
123
|
+
observable.emit(:ready, 1, 2, 3)
|
124
|
+
|
125
|
+
assert callable.verify
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class MixedListenersTest < Minitest::Test
|
130
|
+
test "calls all listeners" do
|
131
|
+
observable = Observable.new
|
132
|
+
callable = Callable.new(:on_ready)
|
133
|
+
another_callable = Callable.new(:on_ready)
|
134
|
+
|
135
|
+
callable.expect(:on_ready, nil, [])
|
136
|
+
another_callable.expect(:on_ready, nil, [])
|
137
|
+
|
138
|
+
observable
|
139
|
+
.add_listener(callable)
|
140
|
+
.on(:ready, &another_callable)
|
141
|
+
.emit(:ready)
|
142
|
+
|
143
|
+
assert callable.verify
|
144
|
+
assert another_callable.verify
|
145
|
+
end
|
146
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "active_record"
|
6
|
+
|
7
|
+
I18n.enforce_available_locales = false
|
8
|
+
|
9
|
+
require "minitest/utils"
|
10
|
+
require "minitest/autorun"
|
11
|
+
|
12
|
+
require "signal"
|
13
|
+
require "support/observable"
|
14
|
+
require "support/observable_with_call"
|
15
|
+
require "support/user"
|
16
|
+
require "support/callable"
|
17
|
+
require "support/emitter"
|
18
|
+
|
19
|
+
ActiveRecord::Base.establish_connection({
|
20
|
+
:adapter => "sqlite3",
|
21
|
+
:database => ":memory:"
|
22
|
+
})
|
23
|
+
|
24
|
+
ActiveRecord::Schema.define(:version => 0) do
|
25
|
+
create_table :users do |t|
|
26
|
+
t.string :username
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: signal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: minitest-utils
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: A simple observer implementation for POROs (Plain Old Ruby Object) and
|
70
84
|
ActiveRecord objects.
|
71
85
|
email:
|
@@ -75,7 +89,6 @@ extensions: []
|
|
75
89
|
extra_rdoc_files: []
|
76
90
|
files:
|
77
91
|
- ".gitignore"
|
78
|
-
- ".rspec"
|
79
92
|
- ".travis.yml"
|
80
93
|
- CHANGELOG.md
|
81
94
|
- Gemfile
|
@@ -86,6 +99,7 @@ files:
|
|
86
99
|
- examples/arguments.rb
|
87
100
|
- examples/block_context.rb
|
88
101
|
- examples/blocks.rb
|
102
|
+
- examples/call.rb
|
89
103
|
- examples/chain.rb
|
90
104
|
- examples/listener.rb
|
91
105
|
- lib/signal.rb
|
@@ -94,17 +108,17 @@ files:
|
|
94
108
|
- lib/signal/listener.rb
|
95
109
|
- lib/signal/version.rb
|
96
110
|
- signal.gemspec
|
97
|
-
-
|
98
|
-
-
|
99
|
-
-
|
100
|
-
-
|
101
|
-
-
|
102
|
-
-
|
103
|
-
-
|
104
|
-
-
|
105
|
-
-
|
106
|
-
-
|
107
|
-
-
|
111
|
+
- test/signal/activerecord_test.rb
|
112
|
+
- test/signal/call_test.rb
|
113
|
+
- test/signal/issues_test.rb
|
114
|
+
- test/signal/listener_test.rb
|
115
|
+
- test/signal/signal_test.rb
|
116
|
+
- test/support/callable.rb
|
117
|
+
- test/support/emitter.rb
|
118
|
+
- test/support/observable.rb
|
119
|
+
- test/support/observable_with_call.rb
|
120
|
+
- test/support/user.rb
|
121
|
+
- test/test_helper.rb
|
108
122
|
homepage: http://github.com/fnando/signal
|
109
123
|
licenses: []
|
110
124
|
metadata: {}
|
@@ -124,20 +138,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
138
|
version: '0'
|
125
139
|
requirements: []
|
126
140
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
141
|
+
rubygems_version: 2.5.1
|
128
142
|
signing_key:
|
129
143
|
specification_version: 4
|
130
144
|
summary: A simple observer implementation for POROs (Plain Old Ruby Object) and ActiveRecord
|
131
145
|
objects.
|
132
146
|
test_files:
|
133
|
-
-
|
134
|
-
-
|
135
|
-
-
|
136
|
-
-
|
137
|
-
-
|
138
|
-
-
|
139
|
-
-
|
140
|
-
-
|
141
|
-
-
|
142
|
-
-
|
143
|
-
-
|
147
|
+
- test/signal/activerecord_test.rb
|
148
|
+
- test/signal/call_test.rb
|
149
|
+
- test/signal/issues_test.rb
|
150
|
+
- test/signal/listener_test.rb
|
151
|
+
- test/signal/signal_test.rb
|
152
|
+
- test/support/callable.rb
|
153
|
+
- test/support/emitter.rb
|
154
|
+
- test/support/observable.rb
|
155
|
+
- test/support/observable_with_call.rb
|
156
|
+
- test/support/user.rb
|
157
|
+
- test/test_helper.rb
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
@@ -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
|
data/spec/signal/call_spec.rb
DELETED
@@ -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
|
data/spec/signal/signal_spec.rb
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Signal do
|
4
|
-
let(:observable) { Observable.new }
|
5
|
-
let(:callable) { Callable.new }
|
6
|
-
|
7
|
-
context 'add listener' do
|
8
|
-
it 'runs listener' do
|
9
|
-
callable.respond_to(:on_ready)
|
10
|
-
observable.add_listener(callable)
|
11
|
-
expect(callable).to receive(:called).with(no_args)
|
12
|
-
|
13
|
-
observable.emit(:ready)
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns itself' do
|
17
|
-
expect(observable.add_listener(callable)).to eq(observable)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'using blocks' do
|
22
|
-
it 'triggers event' do
|
23
|
-
observable.on(:ready, &callable)
|
24
|
-
expect(callable).to receive(:called).with(no_args)
|
25
|
-
|
26
|
-
observable.emit(:ready)
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'triggers event with arguments' do
|
30
|
-
observable.on(:ready, &callable)
|
31
|
-
expect(callable).to receive(:called).with(1, 2, 3)
|
32
|
-
|
33
|
-
observable.emit(:ready, 1, 2, 3)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'triggers before event' do
|
37
|
-
observable.before(:ready, &callable)
|
38
|
-
expect(callable).to receive(:called).with(no_args)
|
39
|
-
|
40
|
-
observable.emit(:ready)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'triggers before event with arguments' do
|
44
|
-
observable.before(:ready, &callable)
|
45
|
-
expect(callable).to receive(:called).with(1, 2, 3)
|
46
|
-
|
47
|
-
observable.emit(:ready, 1, 2, 3)
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'triggers after event' do
|
51
|
-
observable.after(:ready, &callable)
|
52
|
-
expect(callable).to receive(:called).with(no_args)
|
53
|
-
|
54
|
-
observable.emit(:ready)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'triggers after event with arguments' do
|
58
|
-
observable.after(:ready, &callable)
|
59
|
-
expect(callable).to receive(:called).with(1, 2, 3)
|
60
|
-
|
61
|
-
observable.emit(:ready, 1, 2, 3)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'chains events' do
|
65
|
-
before_callable = Callable.new
|
66
|
-
on_callable = Callable.new
|
67
|
-
after_callable = Callable.new
|
68
|
-
|
69
|
-
observable
|
70
|
-
.before(:ready, &before_callable)
|
71
|
-
.on(:ready, &on_callable)
|
72
|
-
.after(:ready, &after_callable)
|
73
|
-
|
74
|
-
expect(before_callable).to receive(:called).with(no_args).ordered
|
75
|
-
expect(on_callable).to receive(:called).with(no_args).ordered
|
76
|
-
expect(after_callable).to receive(:called).with(no_args).ordered
|
77
|
-
|
78
|
-
observable.emit(:ready)
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'keeps context' do
|
82
|
-
context = nil
|
83
|
-
callable = -> { context = self }
|
84
|
-
observable.on(:ready, &callable)
|
85
|
-
observable.emit(:ready)
|
86
|
-
|
87
|
-
expect(context).to eql(self)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'using listeners' do
|
92
|
-
it 'triggers event for listener' do
|
93
|
-
callable.respond_to(:on_ready)
|
94
|
-
observable.listeners << callable
|
95
|
-
expect(callable).to receive(:called).with(no_args)
|
96
|
-
|
97
|
-
observable.emit(:ready)
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'triggers event for listener with arguments' do
|
101
|
-
callable.respond_to(:on_ready)
|
102
|
-
observable.listeners << callable
|
103
|
-
expect(callable).to receive(:called).with(1, 2, 3)
|
104
|
-
|
105
|
-
observable.emit(:ready, 1, 2, 3)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context 'mixed listeners' do
|
110
|
-
it 'calls all listeners' do
|
111
|
-
callable.respond_to(:on_ready)
|
112
|
-
another_callable = Callable.new
|
113
|
-
|
114
|
-
expect(callable).to receive(:called).ordered
|
115
|
-
expect(another_callable).to receive(:called).ordered
|
116
|
-
|
117
|
-
observable
|
118
|
-
.add_listener(callable)
|
119
|
-
.on(:ready, &another_callable)
|
120
|
-
.emit(:ready)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'codeclimate-test-reporter'
|
2
|
-
CodeClimate::TestReporter.start
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'active_record'
|
6
|
-
|
7
|
-
I18n.enforce_available_locales = false
|
8
|
-
|
9
|
-
require 'signal'
|
10
|
-
require 'support/observable'
|
11
|
-
require 'support/observable_with_call'
|
12
|
-
require 'support/callable'
|
13
|
-
require 'support/user'
|
14
|
-
require 'support/emitter'
|
15
|
-
|
16
|
-
ActiveRecord::Base.establish_connection({
|
17
|
-
:adapter => 'sqlite3',
|
18
|
-
:database => ':memory:'
|
19
|
-
})
|
20
|
-
|
21
|
-
ActiveRecord::Schema.define(:version => 0) do
|
22
|
-
create_table :users do |t|
|
23
|
-
t.string :username
|
24
|
-
end
|
25
|
-
end
|