eventish 0.3.2 → 0.4.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: affdd6406fe05bcc51fa60a0bdd1e894c47412e8d8c347e2f8e4e8ed0d846757
4
- data.tar.gz: 754e24d0338cb7eb4c6eb299371d2880b0b867a85fadb0b604ea3511a8e9ee32
3
+ metadata.gz: b680cd59cf6faa489efed4b75bd284699213c249ff2d29c82f62d67facf634a2
4
+ data.tar.gz: 52bb544e7348bf01b9d0b60b236123488a0bdc5cb4e3138dac817292710517a3
5
5
  SHA512:
6
- metadata.gz: 23e2bd4268d4c28a3691327e499552815fbe05d6ada3e17a887e812ec2666d4f65c8c78ac878d7ef0426403cd61760134a06f7c0d9a07da6c8e73fe440da9da0
7
- data.tar.gz: 530d9dcd90223304e1606cbfe909ccf223d1353d1409a230807b29cf6258fa740228235975f0411484b99886ab961349a84ca73a3a432842090c7b95d69cf713
6
+ metadata.gz: 69cc41ae3fb78d9cee65ee21cd71b480534fc0381acedf41bf6d4e26c3d5fe6ec9377359ed44b01b3834af2e2f4a35a39462e2be39f47c672290fc49585e08e7
7
+ data.tar.gz: 98718a8527924fe2e6396fdba0170b5998398aa4427f9b7af92fbf103e4c4a631da4ad9a349ec8131905ec651d3b5225d654200e01445312536313fb68ada425
data/README.md CHANGED
@@ -2,14 +2,16 @@
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
7
  Yet another opinionated events library which proposes a simple API to handle... events 🎉
7
8
 
8
9
  The 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 background execution;
13
+ - with [callbacks wrapper](#callbacks): support ActiveRecord.
14
+ - with [plugins](#plugins): logger and Rails logger included.
13
15
 
14
16
  ## Install
15
17
 
@@ -52,7 +54,7 @@ end
52
54
 
53
55
  For a complete example please take a look at the [dummy app](spec/dummy) in the specs.
54
56
 
55
- ### Adatpers
57
+ ### Adapters
56
58
 
57
59
  Only _ActiveSupport_ is supported for now.
58
60
 
@@ -143,6 +145,32 @@ end
143
145
 
144
146
  The related callback will be setup by the wrapper and the specified event class will be invoked accordingly.
145
147
 
148
+ ### Plugins
149
+
150
+ A plugins system is available for custom processing, a logger and a Rails logger are included in the gem.
151
+
152
+ ```rb
153
+ # initializer setup
154
+ require 'eventish/plugins/rails_logger' # without rails_ for a simple stdout logger
155
+
156
+ Eventish.setup do |config|
157
+ config.before_event = [Eventish::Plugins::RailsLogger]
158
+ config.after_event = [Eventish::Plugins::RailsLogger]
159
+ end
160
+ ```
161
+
162
+ A sample plugin:
163
+
164
+ ```rb
165
+ module Eventish::Plugins::RailsLogger
166
+ class << self
167
+ def call(target, _args, event:, hook: nil, &_block)
168
+ Rails.logger.debug "EVENT: #{hook} #{event.class.event_name} on #{target.inspect}"
169
+ end
170
+ end
171
+ end
172
+ ```
173
+
146
174
  ## Do you like it? Star it!
147
175
 
148
176
  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
@@ -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
@@ -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.2'
4
+ VERSION = '0.4.0'
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
 
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.2
4
+ version: 0.4.0
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-20 00:00:00.000000000 Z
11
+ date: 2022-05-21 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