signal 1.0.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/CODEOWNERS +4 -0
- data/.github/FUNDING.yml +4 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ruby-tests.yml +53 -0
- data/.rubocop.yml +12 -0
- data/Gemfile +3 -1
- data/README.md +87 -43
- data/Rakefile +15 -1
- data/examples/activerecord_model.rb +14 -12
- data/examples/arguments.rb +4 -2
- data/examples/block_context.rb +8 -5
- data/examples/blocks.rb +6 -4
- data/examples/call.rb +23 -0
- data/examples/chain.rb +20 -18
- data/examples/listener.rb +6 -4
- data/lib/signal/extensions/active_record.rb +8 -6
- data/lib/signal/extensions/call.rb +5 -3
- data/lib/signal/listener.rb +4 -1
- data/lib/signal/mock.rb +51 -0
- data/lib/signal/version.rb +3 -1
- data/lib/signal.rb +11 -7
- data/signal.gemspec +39 -17
- metadata +99 -37
- data/.rspec +0 -1
- data/.travis.yml +0 -11
- data/CHANGELOG.md +0 -5
- 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
- data/spec/support/emitter.rb +0 -9
- data/spec/support/observable.rb +0 -3
- data/spec/support/observable_with_call.rb +0 -13
- data/spec/support/user.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 42c616e824230754896cad89c03eec41fc73d84743d93d0f869291a4f9a0305f
|
4
|
+
data.tar.gz: f87c40855eac4f158c70a76441e02dc7e3f28c1d2c3a81ede9d0a008013b1fcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0fa93fccec13f76eb3b3b47a19c5cf24bcd18a3c8a5c185615c6119c4e65241daf3939198e32489973a4e807d14ee80829d3e43a14c42fd9fd8a813cebf3558
|
7
|
+
data.tar.gz: b8ea8d8a3e015ce73f57b337b83df1a27584c439cf92c0c65fdf5a06f0c3cf477362d7d8c05c1a01e1769402d5245b778c523b63debd35a2c6c8c73e3c67d26b
|
data/.github/CODEOWNERS
ADDED
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
---
|
2
|
+
name: "🐛 Bug Report"
|
3
|
+
about: Report a reproducible bug or regression.
|
4
|
+
title: 'Bug: '
|
5
|
+
labels: 'Status: Unconfirmed'
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
<!--
|
10
|
+
- Please provide a clear and concise description of what the bug is.
|
11
|
+
- If possible, add an example reproducing your issue.
|
12
|
+
- Please test using the latest version of rails_env
|
13
|
+
to make sure your issue has not already been fixed.
|
14
|
+
-->
|
15
|
+
|
16
|
+
## Description
|
17
|
+
|
18
|
+
[Add bug description here]
|
19
|
+
|
20
|
+
## How to reproduce
|
21
|
+
|
22
|
+
[Add steps on how to reproduce this issue]
|
23
|
+
|
24
|
+
## What do you expect
|
25
|
+
|
26
|
+
[Describe what do you expect to happen]
|
27
|
+
|
28
|
+
## What happened instead
|
29
|
+
|
30
|
+
[Describe the actual results]
|
31
|
+
|
32
|
+
## Software:
|
33
|
+
|
34
|
+
- Gem version: [Add gem version here]
|
35
|
+
- Ruby version: [Add version here]
|
36
|
+
|
37
|
+
## Full backtrace
|
38
|
+
|
39
|
+
```text
|
40
|
+
[Paste full backtrace here]
|
41
|
+
```
|
@@ -0,0 +1,23 @@
|
|
1
|
+
---
|
2
|
+
name: "💡 Feature request"
|
3
|
+
about: Have an idea that may be useful? Make a suggestion!
|
4
|
+
title: 'Feature Request: '
|
5
|
+
labels: 'Feature request'
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Description
|
10
|
+
|
11
|
+
_A clear and concise description of what the problem is._
|
12
|
+
|
13
|
+
## Describe the solution
|
14
|
+
|
15
|
+
_A clear and concise description of what you want to happen._
|
16
|
+
|
17
|
+
## Alternatives you considered
|
18
|
+
|
19
|
+
_A clear and concise description of any alternative solutions or features you've considered._
|
20
|
+
|
21
|
+
## Additional context
|
22
|
+
|
23
|
+
_Add any other context, screenshots, links, etc about the feature request here._
|
@@ -0,0 +1,38 @@
|
|
1
|
+
<!--
|
2
|
+
If you're making a doc PR or something tiny where the below is irrelevant,
|
3
|
+
delete this template and use a short description, but in your description aim to
|
4
|
+
include both what the change is, and why it is being made, with enough context
|
5
|
+
for anyone to understand.
|
6
|
+
-->
|
7
|
+
|
8
|
+
<details>
|
9
|
+
<summary>PR Checklist</summary>
|
10
|
+
|
11
|
+
### PR Structure
|
12
|
+
|
13
|
+
- [ ] This PR has reasonably narrow scope (if not, break it down into smaller
|
14
|
+
PRs).
|
15
|
+
- [ ] This PR avoids mixing refactoring changes with feature changes (split into
|
16
|
+
two PRs otherwise).
|
17
|
+
- [ ] This PR's title starts is concise and descriptive.
|
18
|
+
|
19
|
+
### Thoroughness
|
20
|
+
|
21
|
+
- [ ] This PR adds tests for the most critical parts of the new functionality or
|
22
|
+
fixes.
|
23
|
+
- [ ] I've updated any docs, `.md` files, etc… affected by this change.
|
24
|
+
|
25
|
+
</details>
|
26
|
+
|
27
|
+
### What
|
28
|
+
|
29
|
+
[TODO: Short statement about what is changing.]
|
30
|
+
|
31
|
+
### Why
|
32
|
+
|
33
|
+
[TODO: Why this change is being made. Include any context required to understand
|
34
|
+
the why.]
|
35
|
+
|
36
|
+
### Known limitations
|
37
|
+
|
38
|
+
[TODO or N/A]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
# Documentation:
|
3
|
+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
4
|
+
|
5
|
+
version: 2
|
6
|
+
updates:
|
7
|
+
- package-ecosystem: "github-actions"
|
8
|
+
directory: "/"
|
9
|
+
schedule:
|
10
|
+
interval: "daily"
|
11
|
+
|
12
|
+
- package-ecosystem: "bundler"
|
13
|
+
directory: "/"
|
14
|
+
schedule:
|
15
|
+
interval: "daily"
|
@@ -0,0 +1,53 @@
|
|
1
|
+
---
|
2
|
+
name: ruby-tests
|
3
|
+
|
4
|
+
on:
|
5
|
+
pull_request:
|
6
|
+
push:
|
7
|
+
workflow_dispatch:
|
8
|
+
inputs: {}
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
build:
|
12
|
+
name: Tests with Ruby ${{ matrix.ruby }} and ${{ matrix.gemfile }}
|
13
|
+
runs-on: "ubuntu-latest"
|
14
|
+
strategy:
|
15
|
+
fail-fast: false
|
16
|
+
matrix:
|
17
|
+
ruby: ["2.7", "3.0"]
|
18
|
+
gemfile:
|
19
|
+
- Gemfile
|
20
|
+
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v2.4.0
|
23
|
+
|
24
|
+
- uses: actions/cache@v2.1.7
|
25
|
+
with:
|
26
|
+
path: vendor/bundle
|
27
|
+
key: >
|
28
|
+
${{ runner.os }}-${{ matrix.ruby }}-gems-${{
|
29
|
+
hashFiles(matrix.gemfile) }}
|
30
|
+
restore-keys: >
|
31
|
+
${{ runner.os }}-${{ matrix.ruby }}-gems-${{
|
32
|
+
hashFiles(matrix.gemfile) }}
|
33
|
+
|
34
|
+
- name: Set up Ruby
|
35
|
+
uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ${{ matrix.ruby }}
|
38
|
+
|
39
|
+
- name: Install gem dependencies
|
40
|
+
env:
|
41
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
42
|
+
run: |
|
43
|
+
gem install bundler
|
44
|
+
bundle config path vendor/bundle
|
45
|
+
bundle update --jobs 4 --retry 3
|
46
|
+
|
47
|
+
- name: Run Tests
|
48
|
+
env:
|
49
|
+
PGHOST: localhost
|
50
|
+
PGUSER: postgres
|
51
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
52
|
+
run: |
|
53
|
+
bundle exec rake
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
# Signal
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
3
|
+
[![Tests](https://github.com/fnando/signal/actions/workflows/ruby-tests.yml/badge.svg)](https://github.com/fnando/signal/actions/workflows/ruby-tests.yml)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/signal.svg)](https://rubygems.org/gems/signal)
|
5
|
+
[![Gem](https://img.shields.io/gem/dt/signal.svg)](https://rubygems.org/gems/signal)
|
6
6
|
|
7
|
-
A simple observer implementation on POROs (Plain Old Ruby Object) and
|
7
|
+
A simple observer implementation on POROs (Plain Old Ruby Object) and
|
8
|
+
ActiveRecord objects.
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
11
12
|
Add this line to your application's Gemfile:
|
12
13
|
|
13
14
|
```ruby
|
14
|
-
gem
|
15
|
+
gem "signal"
|
15
16
|
```
|
16
17
|
|
17
18
|
And then execute:
|
@@ -28,7 +29,8 @@ You can use Signal with PORO (Plain Old Ruby Object) and ActiveRecord.
|
|
28
29
|
|
29
30
|
### Plain Ruby
|
30
31
|
|
31
|
-
All you have to do is including the `Signal` module. Then you can add listeners
|
32
|
+
All you have to do is including the `Signal` module. Then you can add listeners
|
33
|
+
and trigger events.
|
32
34
|
|
33
35
|
```ruby
|
34
36
|
class Status
|
@@ -49,7 +51,8 @@ status.ready!
|
|
49
51
|
#=> After the ready event!
|
50
52
|
```
|
51
53
|
|
52
|
-
You can also pass objects that implement methods like `before_*`, `on_*` and
|
54
|
+
You can also pass objects that implement methods like `before_*`, `on_*` and
|
55
|
+
`after_*`.
|
53
56
|
|
54
57
|
```ruby
|
55
58
|
class MyListener
|
@@ -74,7 +77,9 @@ Status.new
|
|
74
77
|
#=> After the ready event!
|
75
78
|
```
|
76
79
|
|
77
|
-
Executed blocks don't switch context. You always have to emit the object you're
|
80
|
+
Executed blocks don't switch context. You always have to emit the object you're
|
81
|
+
interested in. The follow example uses `emit(:output, self)` to send the
|
82
|
+
`Contact` instance to all listeners.
|
78
83
|
|
79
84
|
```ruby
|
80
85
|
class Contact
|
@@ -119,7 +124,8 @@ Arguments.new
|
|
119
124
|
|
120
125
|
### ActiveRecord
|
121
126
|
|
122
|
-
You can use Signal with ActiveRecord, which will give you some default events
|
127
|
+
You can use Signal with ActiveRecord, which will give you some default events
|
128
|
+
like `:create`, `:update`, `:remove` and `:validation`.
|
123
129
|
|
124
130
|
```ruby
|
125
131
|
class Thing < ActiveRecord::Base
|
@@ -151,22 +157,23 @@ thing.destroy
|
|
151
157
|
|
152
158
|
These are the available events:
|
153
159
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
160
|
+
- `before(:create)`: triggered before creating the record (record is valid).
|
161
|
+
- `on(:create)`: triggered after `before(:create)` event.
|
162
|
+
- `after(:create)`: triggered after the `on(:create)` event.
|
163
|
+
- `before(:update)`: triggered before updating the record (record is valid).
|
164
|
+
- `on(:update)`: triggered when the `before(:update)` event.
|
165
|
+
- `after(:update)`: triggered after the `on(:update)` event.
|
166
|
+
- `before(:remove)`: triggered before removing the record.
|
167
|
+
- `on(:remove)`: triggered after the `before(:remove)`.
|
168
|
+
- `after(:remove)`: triggered after the `on(:remove)` event.
|
169
|
+
- `before(:validation)`: triggered before validating record.
|
170
|
+
- `on(:validation)`: triggered when record is invalid.
|
171
|
+
- `after(:validation)`: triggered after validating record.
|
166
172
|
|
167
173
|
### Inside Rails
|
168
174
|
|
169
|
-
Although there's no special code for Rails, here's just an example of how you
|
175
|
+
Although there's no special code for Rails, here's just an example of how you
|
176
|
+
can use it:
|
170
177
|
|
171
178
|
```ruby
|
172
179
|
class UsersController < ApplicationController
|
@@ -197,7 +204,10 @@ end
|
|
197
204
|
|
198
205
|
### Signal::Call
|
199
206
|
|
200
|
-
You can include `Signal.call` instead, so you can have a common interface for
|
207
|
+
You can include `Signal.call` instead, so you can have a common interface for
|
208
|
+
your observable object. This will add the `.call()` method to the target class,
|
209
|
+
which will delegate attributes to the observable's `initialize` method and call
|
210
|
+
its `call` method.
|
201
211
|
|
202
212
|
```ruby
|
203
213
|
class Contact
|
@@ -219,7 +229,43 @@ Contact.call('John', 'john@example.com') do |o|
|
|
219
229
|
end
|
220
230
|
```
|
221
231
|
|
222
|
-
Notice that you don't have to explicit call the instance's `call` method;
|
232
|
+
Notice that you don't have to explicit call the instance's `call` method;
|
233
|
+
`Contact.call` will initialize the object with all the provided parameters and
|
234
|
+
call `Contact#call` after the block has been executed.
|
235
|
+
|
236
|
+
### Testing
|
237
|
+
|
238
|
+
`Signal::Mock` can be helpful for most test situations where you don't want to
|
239
|
+
bring other mock libraries.
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
require "signal/mock"
|
243
|
+
|
244
|
+
class SomeTest < Minitest::Test
|
245
|
+
def test_some_test
|
246
|
+
mock = Signal::Mock.new
|
247
|
+
|
248
|
+
# Using listener
|
249
|
+
sum = Sum.new
|
250
|
+
sum.add_listener(mock)
|
251
|
+
|
252
|
+
# Calling `mock.on(event_name)` is required because
|
253
|
+
# the handler doesn't receive the event name, just the
|
254
|
+
# arguments.
|
255
|
+
sum = Sum.new
|
256
|
+
sum.on(:result, &mock.on(:result))
|
257
|
+
|
258
|
+
# Using with Signal.call
|
259
|
+
Sum.call(1, 2, &mock)
|
260
|
+
|
261
|
+
assert mock.received?(:result)
|
262
|
+
assert mock.received?(:result, times: 1)
|
263
|
+
assert mock.received?(:result, with: [3])
|
264
|
+
assert mock.received?(:result, with: ->(result) { result == 3 } )
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
```
|
223
269
|
|
224
270
|
## Contributing
|
225
271
|
|
@@ -231,25 +277,23 @@ Notice that you don't have to explicit call the instance's `call` method; `Conta
|
|
231
277
|
|
232
278
|
## License
|
233
279
|
|
234
|
-
Copyright (c) 2013
|
280
|
+
Copyright (c) 2013 Nando Vieira
|
235
281
|
|
236
282
|
MIT License
|
237
283
|
|
238
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
255
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
284
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
285
|
+
this software and associated documentation files (the "Software"), to deal in
|
286
|
+
the Software without restriction, including without limitation the rights to
|
287
|
+
use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies
|
288
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
289
|
+
so, subject to the following conditions:
|
290
|
+
|
291
|
+
The above copyright notice and this permission notice shall be included in all
|
292
|
+
copies or substantial portions of the Software.
|
293
|
+
|
294
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
295
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
296
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
297
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
298
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
299
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
t.warning = false
|
11
|
+
end
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
require 'signal'
|
3
|
-
require 'active_record'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
})
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require "signal"
|
5
|
+
require "active_record"
|
9
6
|
|
10
|
-
ActiveRecord::
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
adapter: "sqlite3",
|
9
|
+
database: ":memory:"
|
10
|
+
)
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(version: 0) do
|
11
13
|
create_table :things do |t|
|
12
14
|
t.string :name
|
13
15
|
t.timestamps null: false
|
@@ -20,7 +22,7 @@ class Thing < ActiveRecord::Base
|
|
20
22
|
validates_presence_of :name
|
21
23
|
end
|
22
24
|
|
23
|
-
thing = Thing.new(:
|
25
|
+
thing = Thing.new(name: "Stuff")
|
24
26
|
thing.on(:create) {|model| puts model.updated_at, model.name }
|
25
27
|
thing.on(:update) {|model| puts model.updated_at, model.name }
|
26
28
|
thing.on(:remove) {|model| puts model.destroyed? }
|
@@ -30,12 +32,12 @@ thing.save!
|
|
30
32
|
#=> 2013-01-26 10:32:39 -0200
|
31
33
|
#=> Stuff
|
32
34
|
|
33
|
-
thing.update_attributes(:
|
35
|
+
thing.update_attributes(name: "Updated stuff")
|
34
36
|
#=> 2013-01-26 10:33:11 -0200
|
35
37
|
#=> Updated stuff
|
36
38
|
|
37
|
-
thing.update_attributes(:
|
38
|
-
#=> [
|
39
|
+
thing.update_attributes(name: nil)
|
40
|
+
#=> ["Name can"t be blank"]
|
39
41
|
|
40
42
|
thing.destroy
|
41
43
|
#=> true
|
data/examples/arguments.rb
CHANGED
data/examples/block_context.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require "signal"
|
3
5
|
|
4
6
|
class Contact
|
5
7
|
include Signal
|
@@ -7,7 +9,8 @@ class Contact
|
|
7
9
|
attr_reader :name, :email
|
8
10
|
|
9
11
|
def initialize(name, email)
|
10
|
-
@name
|
12
|
+
@name = name
|
13
|
+
@email = email
|
11
14
|
end
|
12
15
|
|
13
16
|
def output!
|
@@ -15,6 +18,6 @@ class Contact
|
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
18
|
-
contact = Contact.new(
|
19
|
-
contact.on(:output) {|
|
21
|
+
contact = Contact.new("John Doe", "john@example.org")
|
22
|
+
contact.on(:output) {|c| puts c.name, c.email }
|
20
23
|
contact.output!
|
data/examples/blocks.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
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
|
@@ -10,7 +12,7 @@ class Status
|
|
10
12
|
end
|
11
13
|
|
12
14
|
status = Status.new
|
13
|
-
status.before(:ready) { puts
|
15
|
+
status.before(:ready) { puts "Before the ready event!" }
|
14
16
|
status.on(:ready) { puts "I'm ready!" }
|
15
|
-
status.after(:ready) { puts
|
17
|
+
status.after(:ready) { puts "After the ready event!" }
|
16
18
|
status.ready!
|
data/examples/call.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require "signal"
|
5
|
+
|
6
|
+
class Contact
|
7
|
+
include Signal.call
|
8
|
+
|
9
|
+
attr_reader :name, :email
|
10
|
+
|
11
|
+
def initialize(name, email)
|
12
|
+
@name = name
|
13
|
+
@email = email
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
emit(:output, self)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Contact.call("John", "john@example.com") do |o|
|
22
|
+
o.on(:output) {|contact| puts contact.name }
|
23
|
+
end
|
data/examples/chain.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
require 'signal'
|
3
|
-
require 'active_record'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
})
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require "signal"
|
5
|
+
require "active_record"
|
9
6
|
|
10
|
-
ActiveRecord::
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
adapter: "sqlite3",
|
9
|
+
database: ":memory:"
|
10
|
+
)
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(version: 0) do
|
11
13
|
create_table :things do |t|
|
12
14
|
t.string :name
|
13
15
|
t.timestamps null: false
|
@@ -15,14 +17,14 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
15
17
|
end
|
16
18
|
|
17
19
|
class Thing < ActiveRecord::Base
|
18
|
-
include Signal
|
20
|
+
include Signal.active_record
|
19
21
|
|
20
22
|
validates_presence_of :name
|
21
23
|
end
|
22
24
|
|
23
25
|
class MyListener
|
24
|
-
[
|
25
|
-
class_eval <<-RUBY
|
26
|
+
%i[validation update create remove].each do |type|
|
27
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
26
28
|
def before_#{type}(thing); puts __method__; end
|
27
29
|
def on_#{type}(thing); puts __method__; end
|
28
30
|
def after_#{type}(thing); puts __method__; end
|
@@ -31,26 +33,26 @@ class MyListener
|
|
31
33
|
end
|
32
34
|
|
33
35
|
puts "\n=== Creating valid record"
|
34
|
-
thing = Thing.new(:
|
36
|
+
thing = Thing.new(name: "Stuff")
|
35
37
|
thing.listeners << MyListener.new
|
36
38
|
thing.save
|
37
39
|
|
38
40
|
puts "\n=== Creating invalid record"
|
39
|
-
thing = Thing.new(:
|
41
|
+
thing = Thing.new(name: nil)
|
40
42
|
thing.listeners << MyListener.new
|
41
43
|
thing.save
|
42
44
|
|
43
45
|
puts "\n=== Updating valid record"
|
44
|
-
thing = Thing.create(:
|
46
|
+
thing = Thing.create(name: "Stuff")
|
45
47
|
thing.listeners << MyListener.new
|
46
|
-
thing.update_attributes(:
|
48
|
+
thing.update_attributes(name: "Updated stuff")
|
47
49
|
|
48
50
|
puts "\n=== Updating invalid record"
|
49
|
-
thing = Thing.create!(:
|
51
|
+
thing = Thing.create!(name: "Stuff")
|
50
52
|
thing.listeners << MyListener.new
|
51
|
-
thing.update_attributes(:
|
53
|
+
thing.update_attributes(name: nil)
|
52
54
|
|
53
55
|
puts "\n=== Removing record"
|
54
|
-
thing = Thing.create(:
|
56
|
+
thing = Thing.create(name: "Stuff")
|
55
57
|
thing.listeners << MyListener.new
|
56
58
|
thing.destroy
|