newspaper 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +46 -0
- data/README.md +11 -7
- data/lib/newspaper/version.rb +1 -1
- data/lib/newspaper.rb +7 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f097fb5a67c82b313c8b1e95e4b5e9026c47bbd5bf39eeca2359034de4c26e9d
|
4
|
+
data.tar.gz: 1fd3f3a773ff37aa836649931862cc652eb1b89571965fb319f536888d17659d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6faf338d2c3199e8ab3698ab88351cffb47fed62391fdcf5a3543cfb32124c177cbd8a8a1415d9cb96e20898858f154713cb3ab2faaee1a6227f30cb05b07e08
|
7
|
+
data.tar.gz: 01cce8d7b1e6546dcc9135b50524c5a307e7fab3d37c6a2e82dd3b8fdac234fe45831c6a87e5583fc725fcd4450e67478246e0cea9909b41574622bc2203b114
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
newspaper (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
json (2.6.2)
|
11
|
+
parallel (1.22.1)
|
12
|
+
parser (3.1.2.1)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
power_assert (2.0.1)
|
15
|
+
rainbow (3.1.1)
|
16
|
+
rake (13.0.6)
|
17
|
+
regexp_parser (2.5.0)
|
18
|
+
rexml (3.2.5)
|
19
|
+
rubocop (1.36.0)
|
20
|
+
json (~> 2.3)
|
21
|
+
parallel (~> 1.10)
|
22
|
+
parser (>= 3.1.2.1)
|
23
|
+
rainbow (>= 2.2.2, < 4.0)
|
24
|
+
regexp_parser (>= 1.8, < 3.0)
|
25
|
+
rexml (>= 3.2.5, < 4.0)
|
26
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
27
|
+
ruby-progressbar (~> 1.7)
|
28
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
29
|
+
rubocop-ast (1.21.0)
|
30
|
+
parser (>= 3.1.1.0)
|
31
|
+
ruby-progressbar (1.11.0)
|
32
|
+
test-unit (3.5.3)
|
33
|
+
power_assert
|
34
|
+
unicode-display_width (2.3.0)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
arm64-darwin-21
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
newspaper!
|
41
|
+
rake (~> 13.0)
|
42
|
+
rubocop (~> 1.21)
|
43
|
+
test-unit (~> 3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
2.3.7
|
data/README.md
CHANGED
@@ -20,18 +20,17 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
It can be used as an alternative to ActiveRecord
|
23
|
+
It can be used as an alternative to ActiveRecord Callbacks and Observers. Subscribers are just Ruby objects, so they are easy to implement and test.
|
24
24
|
|
25
25
|
Callbacks and Observers have the following problems
|
26
26
|
|
27
|
-
-
|
28
|
-
-
|
29
|
-
- Often implicitly don't want it to run.
|
27
|
+
- There are often times when you don't want a Callback to be called. We don't want that to happen implicitly.
|
28
|
+
- Easy to write multiple processes in one place. Easy to have dependencies on multiple processes.
|
30
29
|
|
31
30
|
Newspaper solves the above problem by
|
32
31
|
|
33
|
-
-
|
34
|
-
-
|
32
|
+
- Events are explicitly published.
|
33
|
+
- Subscriber is easy to write as a class for each process
|
35
34
|
|
36
35
|
Using ActiveRecord callbacks.
|
37
36
|
|
@@ -52,14 +51,19 @@ end
|
|
52
51
|
If Newspaper is used, it can be written as follows.
|
53
52
|
|
54
53
|
```ruby
|
54
|
+
# app/models/sign_up_notifier.rb
|
55
55
|
class SignUpNotifier
|
56
56
|
def call(payload)
|
57
57
|
# do something
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
# config/initializers/newspaper.rb
|
62
|
+
Rails.configuration.to_prepare do
|
63
|
+
Newspaper.subscribe(:user_create, SignUpNotifier.new)
|
64
|
+
end
|
62
65
|
|
66
|
+
# app/controllers/users_controller.rb
|
63
67
|
Newspaper.publish(:user_create, payload)
|
64
68
|
```
|
65
69
|
|
data/lib/newspaper/version.rb
CHANGED
data/lib/newspaper.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative "newspaper/version"
|
4
4
|
|
5
|
+
# namespace
|
5
6
|
module Newspaper
|
6
7
|
class Error < StandardError; end
|
7
8
|
|
@@ -11,12 +12,15 @@ module Newspaper
|
|
11
12
|
attr_reader :event_bus
|
12
13
|
|
13
14
|
def subscribe(event, subscriber)
|
14
|
-
@event_bus[event]
|
15
|
+
@event_bus[event] ||= []
|
16
|
+
@event_bus[event] << subscriber
|
15
17
|
end
|
16
18
|
|
17
19
|
def publish(event, payload)
|
18
|
-
|
19
|
-
subscriber
|
20
|
+
@event_bus[event] ||= []
|
21
|
+
@event_bus[event].each do |subscriber|
|
22
|
+
subscriber.call(payload)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
def clear(event)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: newspaper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- komagata
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Newspaper is a small library that provides a pub/sub mechanism for ruby.
|
14
14
|
email:
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- CHANGELOG.md
|
22
22
|
- CODE_OF_CONDUCT.md
|
23
23
|
- Gemfile
|
24
|
+
- Gemfile.lock
|
24
25
|
- LICENSE.txt
|
25
26
|
- README.md
|
26
27
|
- Rakefile
|