eventish 0.4.0 → 0.4.1
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/README.md +26 -13
- data/lib/eventish/adapters/active_support.rb +4 -1
- data/lib/eventish/event_api.rb +8 -1
- data/lib/eventish/plugins/rails_logger.rb +1 -1
- data/lib/eventish/version.rb +1 -1
- data/lib/eventish.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04c5125d8999ca2ad1a5ed1a81f7eae2adc4380aa7402cf93c17c8d92e7879f4
|
4
|
+
data.tar.gz: ef364bf047a9c29733793bb3d1b737842f392d414b61430f624d991700d9b560
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f39ee5f5a27fc61ae32a3a7c8747430334a437de11671b8ab6666ad3bbbb6636784d2b1b0a84a249ba5495408f24f75ccae70e350d20c76c7ddf8c77dc685eee
|
7
|
+
data.tar.gz: 6dbbd5ff2495ed5a85263bbbdafe863fba988494426acfcaf142c122084effe60fbbbd557ec61cf780d9c2f2e1c839b6a2183159756093b7e72308f33d1fc84b
|
data/README.md
CHANGED
@@ -4,19 +4,23 @@
|
|
4
4
|
[](https://github.com/blocknotes/eventish/actions/workflows/main.yml)
|
5
5
|
[](https://github.com/blocknotes/eventish/actions/workflows/linters.yml)
|
6
6
|
|
7
|
-
Yet another
|
7
|
+
Yet another events library with a _simple_ API to handle... events :tada:
|
8
8
|
|
9
|
-
|
9
|
+
Main features:
|
10
10
|
- _composable_: just require the components that you need;
|
11
11
|
- with [adapters](#adapters): support ActiveSupport::Notifications for pub/sub events;
|
12
|
-
- with [async events](#async-events): support ActiveJob for background execution;
|
13
|
-
- with [callbacks wrapper](#callbacks): support ActiveRecord.
|
14
|
-
- with [plugins](#plugins): logger and Rails logger included.
|
12
|
+
- with [async events](#async-events): support ActiveJob for events background execution;
|
13
|
+
- with [callbacks wrapper](#callbacks): support ActiveRecord callbacks.
|
14
|
+
- with [plugins](#plugins): a simple logger and a Rails logger are included.
|
15
|
+
|
16
|
+
Please :star: if you like it.
|
17
|
+
|
18
|
+
> You need _eventish_ if you want to speak by events :smile:
|
15
19
|
|
16
20
|
## Install
|
17
21
|
|
18
22
|
- Add to your Gemfile: `gem 'eventish'` (and execute `bundle`)
|
19
|
-
- Create an initializer - _config/initializers/eventish.rb_:
|
23
|
+
- Create an initializer - ex. _config/initializers/eventish.rb_:
|
20
24
|
|
21
25
|
```rb
|
22
26
|
require 'eventish/adapters/active_support'
|
@@ -40,7 +44,7 @@ Rails.configuration.after_initialize do
|
|
40
44
|
end
|
41
45
|
```
|
42
46
|
|
43
|
-
- Create some events - _app/events/main/app_loaded_event.rb_:
|
47
|
+
- Create some events - ex. _app/events/main/app_loaded_event.rb_:
|
44
48
|
|
45
49
|
```rb
|
46
50
|
module Main
|
@@ -56,7 +60,8 @@ For a complete example please take a look at the [dummy app](spec/dummy) in the
|
|
56
60
|
|
57
61
|
### Adapters
|
58
62
|
|
59
|
-
|
63
|
+
The component used events subscription and publishing.
|
64
|
+
Only _ActiveSupport Notification_ is supported for now.
|
60
65
|
|
61
66
|
```rb
|
62
67
|
# initializer setup
|
@@ -80,7 +85,7 @@ Rails.configuration.after_initialize do
|
|
80
85
|
end
|
81
86
|
```
|
82
87
|
|
83
|
-
Sample event - _app/events/main/test_event.rb_:
|
88
|
+
Sample event - ex. _app/events/main/test_event.rb_:
|
84
89
|
|
85
90
|
```rb
|
86
91
|
module Main
|
@@ -99,11 +104,16 @@ module Main
|
|
99
104
|
end
|
100
105
|
```
|
101
106
|
|
102
|
-
Publish the event:
|
107
|
+
Publish the event with:
|
108
|
+
|
109
|
+
```rb
|
110
|
+
Eventish.publish('some_event')
|
111
|
+
```
|
103
112
|
|
104
113
|
### Async events
|
105
114
|
|
106
|
-
Events executed in a background process.
|
115
|
+
Events executed in a background process.
|
116
|
+
Only _ActiveJob_ is supported for now.
|
107
117
|
|
108
118
|
```rb
|
109
119
|
# initializer setup
|
@@ -114,7 +124,7 @@ Rails.configuration.after_initialize do
|
|
114
124
|
end
|
115
125
|
```
|
116
126
|
|
117
|
-
Sample event - _app/events/notifications/user_after_save_commit_event.rb_:
|
127
|
+
Sample event - ex. _app/events/notifications/user_after_save_commit_event.rb_:
|
118
128
|
|
119
129
|
```rb
|
120
130
|
module Notifications
|
@@ -128,7 +138,7 @@ end
|
|
128
138
|
|
129
139
|
### Callbacks
|
130
140
|
|
131
|
-
|
141
|
+
Wrappers for ActiveRecord callbacks using the postfix `_event` (ex. `after_commit_event SomeEvent`).
|
132
142
|
|
133
143
|
```rb
|
134
144
|
# initializer setup
|
@@ -136,6 +146,7 @@ require 'eventish/active_record/callback'
|
|
136
146
|
```
|
137
147
|
|
138
148
|
```rb
|
149
|
+
# sample model
|
139
150
|
class SomeModel < ActiveRecord::Base
|
140
151
|
extend ::Eventish::ActiveRecord::Callback
|
141
152
|
|
@@ -171,6 +182,8 @@ module Eventish::Plugins::RailsLogger
|
|
171
182
|
end
|
172
183
|
```
|
173
184
|
|
185
|
+
Plugins can also be configured for single events overriding _before_event_ and _after_event_.
|
186
|
+
|
174
187
|
## Do you like it? Star it!
|
175
188
|
|
176
189
|
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
|
@@ -15,16 +15,19 @@ module Eventish
|
|
15
15
|
raise ArgumentError, 'Missing event to subscribe' if event.nil?
|
16
16
|
raise ArgumentError, 'Missing handler for subscription' if handler.nil?
|
17
17
|
|
18
|
-
::ActiveSupport::Notifications.subscribe(event.to_s) do |name, start, finish, id, payload|
|
18
|
+
subscriber = ::ActiveSupport::Notifications.subscribe(event.to_s) do |name, start, finish, id, payload|
|
19
19
|
args = { event: name, id: id, start: start, finish: finish }
|
20
20
|
handler.trigger(payload[:target], args, &payload.dig(:options, :block))
|
21
21
|
end
|
22
|
+
Eventish.subscribers[event.to_s] = subscriber
|
23
|
+
subscriber
|
22
24
|
end
|
23
25
|
|
24
26
|
def unsubscribe(event)
|
25
27
|
raise ArgumentError, 'Missing event to unsubscribe' if event.nil?
|
26
28
|
|
27
29
|
::ActiveSupport::Notifications.unsubscribe(event.to_s)
|
30
|
+
Eventish.subscribers.delete(event.to_s)
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
data/lib/eventish/event_api.rb
CHANGED
@@ -28,7 +28,14 @@ module Eventish
|
|
28
28
|
|
29
29
|
def subscribe_all
|
30
30
|
# iterate the descendants
|
31
|
-
|
31
|
+
ignore_events = [Eventish::SimpleEvent]
|
32
|
+
ignore_events.push(Eventish::ActiveJobEvent) if defined? Eventish::ActiveJobEvent
|
33
|
+
events = ObjectSpace.each_object(singleton_class).sort
|
34
|
+
(events - ignore_events).each(&:subscribe)
|
35
|
+
end
|
36
|
+
|
37
|
+
def unsubscribe
|
38
|
+
Eventish.adapter.unsubscribe(event_name)
|
32
39
|
end
|
33
40
|
end
|
34
41
|
end
|
@@ -5,7 +5,7 @@ module Eventish
|
|
5
5
|
class RailsLogger
|
6
6
|
class << self
|
7
7
|
def call(target, _args, event:, hook: nil, &_block)
|
8
|
-
Rails.logger.debug "EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
|
8
|
+
::Rails.logger.debug "EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
data/lib/eventish/version.rb
CHANGED
data/lib/eventish.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple and composable event library
|
14
14
|
email: mat@blocknot.es
|