newspaper 0.1.0 → 0.2.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: bb47099ffa756b5f7ead6ddfd1a8c6b314d610ce1bd748920694f1cd2b7b83cc
4
- data.tar.gz: 7a8f4ebeffb6ac0159d5b868d2a3e412a24f323a01d84a5540c8757df6f5a20b
3
+ metadata.gz: f097fb5a67c82b313c8b1e95e4b5e9026c47bbd5bf39eeca2359034de4c26e9d
4
+ data.tar.gz: 1fd3f3a773ff37aa836649931862cc652eb1b89571965fb319f536888d17659d
5
5
  SHA512:
6
- metadata.gz: 1f0660c73fab5bfa26ac01783b3ceff63e5c61bd3cb422c219e132419ccebabcc0b719bdea4aacfca3bbb9873d7956b2f854c693a692bd15466eea270cabaf8f
7
- data.tar.gz: f07b9507f217c9ceed0fe362621baf06cb2b1a9198c335110e217251090c2930c3b873f7402b6a27a747efb3be36e90c73823bf2745456043f640a267a821096
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 callbacks and Observers. Subscribers are just Ruby objects, so they are easy to implement and test.
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
- - The more callbacks, the more complicated the class becomes.
28
- - It is difficult to understand when callbacks depend on each other.
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
- - By making the subscriber a ruby object, it is easy to divide the process into classes.
34
- - By explicitly publishing events, it is possible to avoid executing events when you do not want them to be executed.
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
- Newspaper.subscribe(:user_create, SignUpNotifier.new)
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Newspaper
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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] = subscriber
15
+ @event_bus[event] ||= []
16
+ @event_bus[event] << subscriber
15
17
  end
16
18
 
17
19
  def publish(event, payload)
18
- subscriber = @event_bus[event]
19
- subscriber.call(payload)
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.1.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-04-29 00:00:00.000000000 Z
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