eventish 0.3.1 → 0.4.1

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: 8f5572d1bca258f0f1c9bd4e09e3743f99bf3045c82013c9742e1b06cfb3c590
4
- data.tar.gz: abe5bdf2cbe1998404c21686ee97758681be8e038c0af03c67b7be1cbed2c8c5
3
+ metadata.gz: 04c5125d8999ca2ad1a5ed1a81f7eae2adc4380aa7402cf93c17c8d92e7879f4
4
+ data.tar.gz: ef364bf047a9c29733793bb3d1b737842f392d414b61430f624d991700d9b560
5
5
  SHA512:
6
- metadata.gz: 885eb9228852f4d143a52ca2db3b622eead0bb9c57bc47fecb8ce29ce9694488973151a1bcaf07df2783192aa362dd3626c200ca36a39afc721a6f5c22cc6222
7
- data.tar.gz: e86ca0a3133a144c709a3a1cc76b2ba777d5995a8068ba202b6570bc4c31ab14e73fea390e3898825276bbc97651dfcdecb978af2266880f47219e810fc00d03
6
+ metadata.gz: f39ee5f5a27fc61ae32a3a7c8747430334a437de11671b8ab6666ad3bbbb6636784d2b1b0a84a249ba5495408f24f75ccae70e350d20c76c7ddf8c77dc685eee
7
+ data.tar.gz: 6dbbd5ff2495ed5a85263bbbdafe863fba988494426acfcaf142c122084effe60fbbbd557ec61cf780d9c2f2e1c839b6a2183159756093b7e72308f33d1fc84b
data/README.md CHANGED
@@ -2,19 +2,25 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/eventish.svg)](https://badge.fury.io/rb/eventish)
4
4
  [![Specs](https://github.com/blocknotes/eventish/actions/workflows/main.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/main.yml)
5
+ [![Linters](https://github.com/blocknotes/eventish/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/linters.yml)
5
6
 
6
- Yet another opinionated events library which proposes a simple API to handle... events 🎉
7
+ Yet another events library with a _simple_ API to handle... events :tada:
7
8
 
8
- The main features:
9
+ Main features:
9
10
  - _composable_: just require the components that you need;
10
- - with _adapters_: support ActiveSupport::Notifications for pub/sub events;
11
- - with _async events_: support ActiveJob for background execution;
12
- - with _callbacks_ wrapper: support ActiveRecord.
11
+ - with [adapters](#adapters): support ActiveSupport::Notifications for pub/sub events;
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:
13
19
 
14
20
  ## Install
15
21
 
16
22
  - Add to your Gemfile: `gem 'eventish'` (and execute `bundle`)
17
- - Create an initializer - _config/initializers/eventish.rb_:
23
+ - Create an initializer - ex. _config/initializers/eventish.rb_:
18
24
 
19
25
  ```rb
20
26
  require 'eventish/adapters/active_support'
@@ -38,7 +44,7 @@ Rails.configuration.after_initialize do
38
44
  end
39
45
  ```
40
46
 
41
- - Create some events - _app/events/main/app_loaded_event.rb_:
47
+ - Create some events - ex. _app/events/main/app_loaded_event.rb_:
42
48
 
43
49
  ```rb
44
50
  module Main
@@ -52,9 +58,10 @@ end
52
58
 
53
59
  For a complete example please take a look at the [dummy app](spec/dummy) in the specs.
54
60
 
55
- ### Adatpers
61
+ ### Adapters
56
62
 
57
- Only _ActiveSupport_ is supported for now.
63
+ The component used events subscription and publishing.
64
+ Only _ActiveSupport Notification_ is supported for now.
58
65
 
59
66
  ```rb
60
67
  # initializer setup
@@ -78,7 +85,7 @@ Rails.configuration.after_initialize do
78
85
  end
79
86
  ```
80
87
 
81
- Sample event - _app/events/main/test_event.rb_:
88
+ Sample event - ex. _app/events/main/test_event.rb_:
82
89
 
83
90
  ```rb
84
91
  module Main
@@ -97,11 +104,16 @@ module Main
97
104
  end
98
105
  ```
99
106
 
100
- Publish the event: `Eventish.publish('some_event')`
107
+ Publish the event with:
108
+
109
+ ```rb
110
+ Eventish.publish('some_event')
111
+ ```
101
112
 
102
113
  ### Async events
103
114
 
104
- Events executed in a background process. Only _ActiveJob_ is supported for now.
115
+ Events executed in a background process.
116
+ Only _ActiveJob_ is supported for now.
105
117
 
106
118
  ```rb
107
119
  # initializer setup
@@ -112,7 +124,7 @@ Rails.configuration.after_initialize do
112
124
  end
113
125
  ```
114
126
 
115
- Sample event - _app/events/notifications/user_after_save_commit_event.rb_:
127
+ Sample event - ex. _app/events/notifications/user_after_save_commit_event.rb_:
116
128
 
117
129
  ```rb
118
130
  module Notifications
@@ -126,7 +138,7 @@ end
126
138
 
127
139
  ### Callbacks
128
140
 
129
- Only for ActiveRecord, callbacks wrapper are available with the postfix `_event` (ex. `after_commit_event SomeEvent`).
141
+ Wrappers for ActiveRecord callbacks using the postfix `_event` (ex. `after_commit_event SomeEvent`).
130
142
 
131
143
  ```rb
132
144
  # initializer setup
@@ -134,6 +146,7 @@ require 'eventish/active_record/callback'
134
146
  ```
135
147
 
136
148
  ```rb
149
+ # sample model
137
150
  class SomeModel < ActiveRecord::Base
138
151
  extend ::Eventish::ActiveRecord::Callback
139
152
 
@@ -143,6 +156,34 @@ end
143
156
 
144
157
  The related callback will be setup by the wrapper and the specified event class will be invoked accordingly.
145
158
 
159
+ ### Plugins
160
+
161
+ A plugins system is available for custom processing, a logger and a Rails logger are included in the gem.
162
+
163
+ ```rb
164
+ # initializer setup
165
+ require 'eventish/plugins/rails_logger' # without rails_ for a simple stdout logger
166
+
167
+ Eventish.setup do |config|
168
+ config.before_event = [Eventish::Plugins::RailsLogger]
169
+ config.after_event = [Eventish::Plugins::RailsLogger]
170
+ end
171
+ ```
172
+
173
+ A sample plugin:
174
+
175
+ ```rb
176
+ module Eventish::Plugins::RailsLogger
177
+ class << self
178
+ def call(target, _args, event:, hook: nil, &_block)
179
+ Rails.logger.debug "EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
180
+ end
181
+ end
182
+ end
183
+ ```
184
+
185
+ Plugins can also be configured for single events overriding _before_event_ and _after_event_.
186
+
146
187
  ## Do you like it? Star it!
147
188
 
148
189
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
@@ -7,7 +7,10 @@ module Eventish
7
7
  end
8
8
 
9
9
  def perform(target, args)
10
+ self.class.before_event.each { |plugin| plugin.call(target, args, event: self, hook: :before) }
10
11
  call(target, args)
12
+ self.class.after_event.each { |plugin| plugin.call(target, args, event: self, hook: :after) }
13
+ self
11
14
  end
12
15
 
13
16
  class << self
@@ -4,12 +4,12 @@ module Eventish
4
4
  module ActiveRecord
5
5
  module Callback
6
6
  # Init events
7
- def after_initialize_event
8
- before_validation -> { ::Eventish.publish(event, self) }
7
+ def after_initialize_event(event)
8
+ after_initialize -> { ::Eventish.publish(event, self) }
9
9
  end
10
10
 
11
- def after_find_event
12
- before_validation -> { ::Eventish.publish(event, self) }
11
+ def after_find_event(event)
12
+ after_find -> { ::Eventish.publish(event, self) }
13
13
  end
14
14
 
15
15
  # Validation events
@@ -27,7 +27,7 @@ module Eventish
27
27
  end
28
28
 
29
29
  def around_create_event(event)
30
- around_create -> { ::Eventish.publish(event, self) }
30
+ around_create ->(_object, block) { ::Eventish.publish(event, self, block: block) }
31
31
  end
32
32
 
33
33
  def after_create_event(event)
@@ -40,7 +40,7 @@ module Eventish
40
40
  end
41
41
 
42
42
  def around_update_event(event)
43
- around_update -> { ::Eventish.publish(event, self) }
43
+ around_update ->(_object, block) { ::Eventish.publish(event, self, block: block) }
44
44
  end
45
45
 
46
46
  def after_update_event(event)
@@ -53,7 +53,7 @@ module Eventish
53
53
  end
54
54
 
55
55
  def around_save_event(event)
56
- around_save -> { ::Eventish.publish(event, self) }
56
+ around_save ->(_object, block) { ::Eventish.publish(event, self, block: block) }
57
57
  end
58
58
 
59
59
  def after_save_event(event)
@@ -66,7 +66,7 @@ module Eventish
66
66
  end
67
67
 
68
68
  def around_destroy_event(event)
69
- around_destroy -> { ::Eventish.publish(event, self) }
69
+ around_destroy ->(_object, block) { ::Eventish.publish(event, self, block: block) }
70
70
  end
71
71
 
72
72
  def after_destroy_event(event)
@@ -78,10 +78,6 @@ module Eventish
78
78
  after_commit -> { ::Eventish.publish(event, self) }
79
79
  end
80
80
 
81
- def after_save_commit_event(event)
82
- after_save_commit -> { ::Eventish.publish(event, self) }
83
- end
84
-
85
81
  def after_create_commit_event(event)
86
82
  after_create_commit -> { ::Eventish.publish(event, self) }
87
83
  end
@@ -90,6 +86,10 @@ module Eventish
90
86
  after_update_commit -> { ::Eventish.publish(event, self) }
91
87
  end
92
88
 
89
+ def after_save_commit_event(event)
90
+ after_save_commit -> { ::Eventish.publish(event, self) }
91
+ end
92
+
93
93
  def after_destroy_commit_event(event)
94
94
  after_destroy_commit -> { ::Eventish.publish(event, self) }
95
95
  end
@@ -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
@@ -6,6 +6,14 @@ module Eventish
6
6
  other&.priority <=> priority
7
7
  end
8
8
 
9
+ def after_event
10
+ Eventish.config.after_event || []
11
+ end
12
+
13
+ def before_event
14
+ Eventish.config.before_event || []
15
+ end
16
+
9
17
  def event_name
10
18
  @event_name ||= to_s
11
19
  end
@@ -20,7 +28,14 @@ module Eventish
20
28
 
21
29
  def subscribe_all
22
30
  # iterate the descendants
23
- ObjectSpace.each_object(singleton_class).sort.each(&:subscribe)
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)
24
39
  end
25
40
  end
26
41
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eventish
4
+ module Plugins
5
+ class Logger
6
+ class << self
7
+ def call(target, _args, event:, hook: nil, &_block)
8
+ puts "[#{Time.now}] EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eventish
4
+ module Plugins
5
+ class RailsLogger
6
+ class << self
7
+ def call(target, _args, event:, hook: nil, &_block)
8
+ ::Rails.logger.debug "EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -10,7 +10,11 @@ module Eventish
10
10
  include EventApi
11
11
 
12
12
  def trigger(target, args, &block)
13
- new.call(target, args, &block)
13
+ event = new
14
+ before_event.each { |plugin| plugin.call(target, args, event: event, hook: :before, &block) }
15
+ event.call(target, args, &block)
16
+ after_event.each { |plugin| plugin.call(target, args, event: event, hook: :after, &block) }
17
+ event
14
18
  end
15
19
  end
16
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eventish # :nodoc:
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/eventish.rb CHANGED
@@ -4,7 +4,7 @@ require_relative 'eventish/event_api'
4
4
  require_relative 'eventish/simple_event'
5
5
 
6
6
  module Eventish
7
- OPTIONS = %i[adapter].freeze
7
+ OPTIONS = %i[adapter after_event before_event].freeze
8
8
 
9
9
  AdapterError = Class.new(StandardError)
10
10
 
@@ -29,4 +29,8 @@ module Eventish
29
29
 
30
30
  @options
31
31
  end
32
+
33
+ def subscribers
34
+ @subscribers ||= {}
35
+ end
32
36
  end
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.3.1
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-19 00:00:00.000000000 Z
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
@@ -23,6 +23,8 @@ files:
23
23
  - lib/eventish/active_record/callback.rb
24
24
  - lib/eventish/adapters/active_support.rb
25
25
  - lib/eventish/event_api.rb
26
+ - lib/eventish/plugins/logger.rb
27
+ - lib/eventish/plugins/rails_logger.rb
26
28
  - lib/eventish/simple_event.rb
27
29
  - lib/eventish/version.rb
28
30
  homepage: https://github.com/blocknotes/eventish