eventish 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08c9cf6785fc35a3174bf04df8a3f46011a10f1dc465ed24903d3924c293927a'
4
- data.tar.gz: 53efbd04a12d69149c594f375b3b79da0cda0a9ed6a339e09d88ff4a9d4d830b
3
+ metadata.gz: 8f5572d1bca258f0f1c9bd4e09e3743f99bf3045c82013c9742e1b06cfb3c590
4
+ data.tar.gz: abe5bdf2cbe1998404c21686ee97758681be8e038c0af03c67b7be1cbed2c8c5
5
5
  SHA512:
6
- metadata.gz: 3aabb2135effcd4234379ef10beda4cb8cd2a5489b6b7318e7bc9cc84de6ef84daa5972b35496636dc3d439f7829cf2ef3ef27786c7512339a24b87404b4bce8
7
- data.tar.gz: 63197732c22986bfc7f9d647808350886b71121cc9ccd3125ba35fa3f6eb3db2cc152b36c4f0fbd347224c46b9be50381a18e595014088341879769590593535
6
+ metadata.gz: 885eb9228852f4d143a52ca2db3b622eead0bb9c57bc47fecb8ce29ce9694488973151a1bcaf07df2783192aa362dd3626c200ca36a39afc721a6f5c22cc6222
7
+ data.tar.gz: e86ca0a3133a144c709a3a1cc76b2ba777d5995a8068ba202b6570bc4c31ab14e73fea390e3898825276bbc97651dfcdecb978af2266880f47219e810fc00d03
data/README.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # Eventish
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/eventish.svg)](https://badge.fury.io/rb/eventish)
4
+ [![Specs](https://github.com/blocknotes/eventish/actions/workflows/main.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/main.yml)
5
+
3
6
  Yet another opinionated events library which proposes a simple API to handle... events 🎉
4
7
 
5
8
  The main features:
6
- - composable: just require the components that you need;
7
- - with adapters: support _ActiveSupport::Notifications_;
8
- - with async events: support _ActiveJob_;
9
- - with callbacks: support _ActiveRecord_.
9
+ - _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.
10
13
 
11
14
  ## Install
12
15
 
@@ -31,7 +34,7 @@ end
31
34
  Rails.configuration.after_initialize do
32
35
  Eventish::SimpleEvent.subscribe_all # NOTE: events will be available after this point
33
36
 
34
- Eventish.adapter.publish('app_loaded') # just a test event
37
+ Eventish.publish('app_loaded') # just a test event
35
38
  end
36
39
  ```
37
40
 
@@ -86,8 +89,7 @@ module Main
86
89
 
87
90
  class << self
88
91
  def event_name
89
- # this is optional, if not set the event name will be inferred from the class name
90
- # in this sample it would be "test" - TestEvent => underscore => remove _event suffix
92
+ # this is optional, if not set the class name to string will be used
91
93
  'some_event'
92
94
  end
93
95
  end
@@ -95,7 +97,7 @@ module Main
95
97
  end
96
98
  ```
97
99
 
98
- Publish the event: `Eventish.adapter.publish('some_event')`
100
+ Publish the event: `Eventish.publish('some_event')`
99
101
 
100
102
  ### Async events
101
103
 
@@ -124,24 +126,22 @@ end
124
126
 
125
127
  ### Callbacks
126
128
 
127
- Wrapper for callbacks. Only _ActiveRecord_ is supported for now.
128
- This is just a glue component to have callbacks with a nice syntax.
129
+ Only for ActiveRecord, callbacks wrapper are available with the postfix `_event` (ex. `after_commit_event SomeEvent`).
129
130
 
130
131
  ```rb
131
132
  # initializer setup
132
- require 'eventish/callback'
133
+ require 'eventish/active_record/callback'
133
134
  ```
134
135
 
135
- Sample model usage:
136
-
137
136
  ```rb
138
- class User < ActiveRecord::Base
139
- before_validation ::Eventish::Callback
140
- after_save_commit ::Eventish::Callback
137
+ class SomeModel < ActiveRecord::Base
138
+ extend ::Eventish::ActiveRecord::Callback
139
+
140
+ before_validation_event SomeBeforeValidationEvent
141
141
  end
142
142
  ```
143
143
 
144
- For events definition see Simple or Async events.
144
+ The related callback will be setup by the wrapper and the specified event class will be invoked accordingly.
145
145
 
146
146
  ## Do you like it? Star it!
147
147
 
@@ -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
8
+ before_validation -> { ::Eventish.publish(event, self) }
9
+ end
10
+
11
+ def after_find_event
12
+ before_validation -> { ::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 -> { ::Eventish.publish(event, self) }
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 -> { ::Eventish.publish(event, self) }
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 -> { ::Eventish.publish(event, self) }
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 -> { ::Eventish.publish(event, self) }
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_save_commit_event(event)
82
+ after_save_commit -> { ::Eventish.publish(event, self) }
83
+ end
84
+
85
+ def after_create_commit_event(event)
86
+ after_create_commit -> { ::Eventish.publish(event, self) }
87
+ end
88
+
89
+ def after_update_commit_event(event)
90
+ after_update_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
@@ -4,19 +4,27 @@ module Eventish
4
4
  class Adapters
5
5
  class ActiveSupport
6
6
  class << self
7
- def publish(event_name, target = nil, options: {})
8
- ::ActiveSupport::Notifications.instrument(event_name, target: target, event_options: options)
7
+ def publish(event, target = nil, block: nil)
8
+ raise ArgumentError, 'Missing event to publish' if event.nil?
9
+
10
+ options = { block: block }
11
+ ::ActiveSupport::Notifications.instrument(event.to_s, target: target, options: options)
9
12
  end
10
13
 
11
- def subscribe(event_name, handler)
12
- ::ActiveSupport::Notifications.subscribe(event_name) do |name, start, finish, id, payload|
14
+ def subscribe(event, handler)
15
+ raise ArgumentError, 'Missing event to subscribe' if event.nil?
16
+ raise ArgumentError, 'Missing handler for subscription' if handler.nil?
17
+
18
+ ::ActiveSupport::Notifications.subscribe(event.to_s) do |name, start, finish, id, payload|
13
19
  args = { event: name, id: id, start: start, finish: finish }
14
- handler.trigger(payload[:target], args, &payload[:block])
20
+ handler.trigger(payload[:target], args, &payload.dig(:options, :block))
15
21
  end
16
22
  end
17
23
 
18
- def unsubscribe(event_name)
19
- ::ActiveSupport::Notifications.unsubscribe(event_name)
24
+ def unsubscribe(event)
25
+ raise ArgumentError, 'Missing event to unsubscribe' if event.nil?
26
+
27
+ ::ActiveSupport::Notifications.unsubscribe(event.to_s)
20
28
  end
21
29
  end
22
30
  end
@@ -7,8 +7,7 @@ module Eventish
7
7
  end
8
8
 
9
9
  def event_name
10
- # If event_name is not set, infer the event from the class name
11
- @event_name ||= Eventish.underscore(to_s).delete_suffix('_event')
10
+ @event_name ||= to_s
12
11
  end
13
12
 
14
13
  def priority
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eventish # :nodoc:
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.1'
5
5
  end
data/lib/eventish.rb CHANGED
@@ -6,7 +6,7 @@ require_relative 'eventish/simple_event'
6
6
  module Eventish
7
7
  OPTIONS = %i[adapter].freeze
8
8
 
9
- MissingAdapterError = Class.new(StandardError)
9
+ AdapterError = Class.new(StandardError)
10
10
 
11
11
  module_function
12
12
 
@@ -18,21 +18,15 @@ module Eventish
18
18
  @options ||= Struct.new(*OPTIONS).new # rubocop:disable Naming/MemoizedInstanceVariableName
19
19
  end
20
20
 
21
+ def publish(event_name, target = nil, block: nil)
22
+ config.adapter&.publish(event_name, target, block: block)
23
+ end
24
+
21
25
  def setup
22
26
  @options ||= Struct.new(*OPTIONS).new
23
27
  yield(@options) if block_given?
24
- raise MissingAdapterError, 'Please specify an event adapter' unless @options.adapter
28
+ raise AdapterError, 'Please specify an event adapter' unless @options.adapter
25
29
 
26
30
  @options
27
31
  end
28
-
29
- def underscore(camel_cased_word)
30
- return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)
31
-
32
- word = camel_cased_word.to_s.gsub(/\A.*::/, '')
33
- word.gsub!(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
34
- word.tr!("-", "_")
35
- word.downcase!
36
- word
37
- end
38
32
  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.1.0
4
+ version: 0.3.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-17 00:00:00.000000000 Z
11
+ date: 2022-05-19 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,8 @@ 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
- - lib/eventish/callback.rb
25
25
  - lib/eventish/event_api.rb
26
26
  - lib/eventish/simple_event.rb
27
27
  - lib/eventish/version.rb
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eventish
4
- class Callback
5
- class << self
6
- def callback_event(target, &block)
7
- event = "#{Eventish.underscore(target.class.to_s)}_#{__callee__}"
8
- Eventish.adapter.publish(event, target, &block)
9
- end
10
-
11
- def callback_commit_event(target, &block)
12
- event = "#{Eventish.underscore(target.class.to_s)}_after_commit"
13
- Eventish.adapter.publish(event, target, &block)
14
- end
15
-
16
- alias_method :after_initialize, :callback_event
17
- alias_method :after_find, :callback_event
18
-
19
- alias_method :before_validation, :callback_event
20
- alias_method :after_validation, :callback_event
21
-
22
- alias_method :before_create, :callback_event
23
- alias_method :around_create, :callback_event
24
- alias_method :after_create, :callback_event
25
-
26
- alias_method :before_update, :callback_event
27
- alias_method :around_update, :callback_event
28
- alias_method :after_update, :callback_event
29
-
30
- alias_method :before_save, :callback_event
31
- alias_method :around_save, :callback_event
32
- alias_method :after_save, :callback_event
33
-
34
- alias_method :before_destroy, :callback_event
35
- alias_method :around_destroy, :callback_event
36
- alias_method :after_destroy, :callback_event
37
-
38
- alias_method :after_commit, :callback_commit_event
39
- alias_method :after_save_commit, :callback_commit_event # => after_commit
40
- alias_method :after_create_commit, :callback_commit_event # => after_commit
41
- alias_method :after_update_commit, :callback_commit_event # => after_commit
42
- alias_method :after_destroy_commit, :callback_commit_event # => after_commit
43
- alias_method :after_rollback, :callback_event
44
-
45
- alias_method :after_touch, :callback_event
46
- end
47
- end
48
- end