eventish 0.3.0 → 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: 965405052bda0cb9903d4a447c91e5db89c8b98a868ded8428ee0399652680bd
4
- data.tar.gz: 4608f89362bd42919071b404d428c8a44178d608a51ae0c0eb2aeb0b9a3d40ee
3
+ metadata.gz: b680cd59cf6faa489efed4b75bd284699213c249ff2d29c82f62d67facf634a2
4
+ data.tar.gz: 52bb544e7348bf01b9d0b60b236123488a0bdc5cb4e3138dac817292710517a3
5
5
  SHA512:
6
- metadata.gz: 78839ea7b019d89e6587692faa575e345c9b72eae8355833cb374a5b7906cedd97ebcc74e4e61db1ecf9ea3c4764f5de7eeacac0a79bd6448c080f341ef33db3
7
- data.tar.gz: 9b88eb7b02e330c68449cb7e978c8b40c6bc0a19cab874bd082fbf897ef61a9ec67ad3d2be866b3936d0370f777f158d423a414d3f469a4656facdfb8d201cb1
6
+ metadata.gz: 69cc41ae3fb78d9cee65ee21cd71b480534fc0381acedf41bf6d4e26c3d5fe6ec9377359ed44b01b3834af2e2f4a35a39462e2be39f47c672290fc49585e08e7
7
+ data.tar.gz: 98718a8527924fe2e6396fdba0170b5998398aa4427f9b7af92fbf103e4c4a631da4ad9a349ec8131905ec651d3b5225d654200e01445312536313fb68ada425
data/README.md CHANGED
@@ -2,13 +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.
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
15
 
13
16
  ## Install
14
17
 
@@ -51,7 +54,7 @@ end
51
54
 
52
55
  For a complete example please take a look at the [dummy app](spec/dummy) in the specs.
53
56
 
54
- ### Adatpers
57
+ ### Adapters
55
58
 
56
59
  Only _ActiveSupport_ is supported for now.
57
60
 
@@ -123,6 +126,51 @@ module Notifications
123
126
  end
124
127
  ```
125
128
 
129
+ ### Callbacks
130
+
131
+ Only for ActiveRecord, callbacks wrapper are available with the postfix `_event` (ex. `after_commit_event SomeEvent`).
132
+
133
+ ```rb
134
+ # initializer setup
135
+ require 'eventish/active_record/callback'
136
+ ```
137
+
138
+ ```rb
139
+ class SomeModel < ActiveRecord::Base
140
+ extend ::Eventish::ActiveRecord::Callback
141
+
142
+ before_validation_event SomeBeforeValidationEvent
143
+ end
144
+ ```
145
+
146
+ The related callback will be setup by the wrapper and the specified event class will be invoked accordingly.
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
+
126
174
  ## Do you like it? Star it!
127
175
 
128
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
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eventish
4
+ module ActiveRecord
5
+ module Callback
6
+ # Init events
7
+ def after_initialize_event(event)
8
+ after_initialize -> { ::Eventish.publish(event, self) }
9
+ end
10
+
11
+ def after_find_event(event)
12
+ after_find -> { ::Eventish.publish(event, self) }
13
+ end
14
+
15
+ # Validation events
16
+ def before_validation_event(event)
17
+ before_validation -> { ::Eventish.publish(event, self) }
18
+ end
19
+
20
+ def after_validation_event(event)
21
+ after_validation -> { ::Eventish.publish(event, self) }
22
+ end
23
+
24
+ # Create events
25
+ def before_create_event(event)
26
+ before_create -> { ::Eventish.publish(event, self) }
27
+ end
28
+
29
+ def around_create_event(event)
30
+ around_create ->(_object, block) { ::Eventish.publish(event, self, block: block) }
31
+ end
32
+
33
+ def after_create_event(event)
34
+ after_create -> { ::Eventish.publish(event, self) }
35
+ end
36
+
37
+ # Update events
38
+ def before_update_event(event)
39
+ before_update -> { ::Eventish.publish(event, self) }
40
+ end
41
+
42
+ def around_update_event(event)
43
+ around_update ->(_object, block) { ::Eventish.publish(event, self, block: block) }
44
+ end
45
+
46
+ def after_update_event(event)
47
+ after_update -> { ::Eventish.publish(event, self) }
48
+ end
49
+
50
+ # Save events
51
+ def before_save_event(event)
52
+ before_save -> { ::Eventish.publish(event, self) }
53
+ end
54
+
55
+ def around_save_event(event)
56
+ around_save ->(_object, block) { ::Eventish.publish(event, self, block: block) }
57
+ end
58
+
59
+ def after_save_event(event)
60
+ after_save -> { ::Eventish.publish(event, self) }
61
+ end
62
+
63
+ # Destroy events
64
+ def before_destroy_event(event)
65
+ before_destroy -> { ::Eventish.publish(event, self) }
66
+ end
67
+
68
+ def around_destroy_event(event)
69
+ around_destroy ->(_object, block) { ::Eventish.publish(event, self, block: block) }
70
+ end
71
+
72
+ def after_destroy_event(event)
73
+ after_destroy -> { ::Eventish.publish(event, self) }
74
+ end
75
+
76
+ # Commit events
77
+ def after_commit_event(event)
78
+ after_commit -> { ::Eventish.publish(event, self) }
79
+ end
80
+
81
+ def after_create_commit_event(event)
82
+ after_create_commit -> { ::Eventish.publish(event, self) }
83
+ end
84
+
85
+ def after_update_commit_event(event)
86
+ after_update_commit -> { ::Eventish.publish(event, self) }
87
+ end
88
+
89
+ def after_save_commit_event(event)
90
+ after_save_commit -> { ::Eventish.publish(event, self) }
91
+ end
92
+
93
+ def after_destroy_commit_event(event)
94
+ after_destroy_commit -> { ::Eventish.publish(event, self) }
95
+ end
96
+
97
+ def after_rollback_event(event)
98
+ after_rollback -> { ::Eventish.publish(event, self) }
99
+ end
100
+
101
+ # Touch events
102
+ def after_touch_event(event)
103
+ after_touch -> { ::Eventish.publish(event, self) }
104
+ end
105
+ end
106
+ end
107
+ 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
@@ -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.0'
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.0
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-18 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
@@ -20,8 +20,11 @@ files:
20
20
  - README.md
21
21
  - lib/eventish.rb
22
22
  - lib/eventish/active_job_event.rb
23
+ - lib/eventish/active_record/callback.rb
23
24
  - lib/eventish/adapters/active_support.rb
24
25
  - lib/eventish/event_api.rb
26
+ - lib/eventish/plugins/logger.rb
27
+ - lib/eventish/plugins/rails_logger.rb
25
28
  - lib/eventish/simple_event.rb
26
29
  - lib/eventish/version.rb
27
30
  homepage: https://github.com/blocknotes/eventish