eventish 0.2.0 → 0.3.2

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: af7a6bf91a4b7cc8af46ea2520aeef223eedcce5cedceac04b7d91cc43333936
4
- data.tar.gz: 1ab3f8d60e00c982c3044cf0fc7bf7e53dd1435914238842724d6f2f211698ba
3
+ metadata.gz: affdd6406fe05bcc51fa60a0bdd1e894c47412e8d8c347e2f8e4e8ed0d846757
4
+ data.tar.gz: 754e24d0338cb7eb4c6eb299371d2880b0b867a85fadb0b604ea3511a8e9ee32
5
5
  SHA512:
6
- metadata.gz: dd0f4b9d8d6ec2705bd0d6679296705b5e3838730fc3fbcaa6657490dc6c108db80886cf2399220545b0b374c904da0bd1ac19beaac3e9c7af9a3efb5c625bc9
7
- data.tar.gz: e1eb63690fde2b62cbfc0d9c52bb1beea73e03205abffef5aa24de8097c4e935d6ee15c2ee67d19dc6acd46050ef11d8b85b11cbcbd5caa86e24f6199f7c5fe2
6
+ metadata.gz: 23e2bd4268d4c28a3691327e499552815fbe05d6ada3e17a887e812ec2666d4f65c8c78ac878d7ef0426403cd61760134a06f7c0d9a07da6c8e73fe440da9da0
7
+ data.tar.gz: 530d9dcd90223304e1606cbfe909ccf223d1353d1409a230807b29cf6258fa740228235975f0411484b99886ab961349a84ca73a3a432842090c7b95d69cf713
data/README.md CHANGED
@@ -8,7 +8,8 @@ Yet another opinionated events library which proposes a simple API to handle...
8
8
  The main features:
9
9
  - _composable_: just require the components that you need;
10
10
  - with _adapters_: support ActiveSupport::Notifications for pub/sub events;
11
- - with _async events_: support ActiveJob for background execution.
11
+ - with _async events_: support ActiveJob for background execution;
12
+ - with _callbacks_ wrapper: support ActiveRecord.
12
13
 
13
14
  ## Install
14
15
 
@@ -88,8 +89,7 @@ module Main
88
89
 
89
90
  class << self
90
91
  def event_name
91
- # this is optional, if not set the event name will be inferred from the class name
92
- # 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
93
93
  'some_event'
94
94
  end
95
95
  end
@@ -124,6 +124,25 @@ module Notifications
124
124
  end
125
125
  ```
126
126
 
127
+ ### Callbacks
128
+
129
+ Only for ActiveRecord, callbacks wrapper are available with the postfix `_event` (ex. `after_commit_event SomeEvent`).
130
+
131
+ ```rb
132
+ # initializer setup
133
+ require 'eventish/active_record/callback'
134
+ ```
135
+
136
+ ```rb
137
+ class SomeModel < ActiveRecord::Base
138
+ extend ::Eventish::ActiveRecord::Callback
139
+
140
+ before_validation_event SomeBeforeValidationEvent
141
+ end
142
+ ```
143
+
144
+ The related callback will be setup by the wrapper and the specified event class will be invoked accordingly.
145
+
127
146
  ## Do you like it? Star it!
128
147
 
129
148
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
@@ -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
@@ -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.2.0'
4
+ VERSION = '0.3.2'
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,25 +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)
22
- config.adapter&.publish(event_name, target, &block)
21
+ def publish(event_name, target = nil, block: nil)
22
+ config.adapter&.publish(event_name, target, block: block)
23
23
  end
24
24
 
25
25
  def setup
26
26
  @options ||= Struct.new(*OPTIONS).new
27
27
  yield(@options) if block_given?
28
- raise MissingAdapterError, 'Please specify an event adapter' unless @options.adapter
28
+ raise AdapterError, 'Please specify an event adapter' unless @options.adapter
29
29
 
30
30
  @options
31
31
  end
32
-
33
- def underscore(camel_cased_word)
34
- return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)
35
-
36
- word = camel_cased_word.to_s.gsub(/\A.*::/, '')
37
- word.gsub!(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
38
- word.tr!("-", "_")
39
- word.downcase!
40
- word
41
- end
42
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.2.0
4
+ version: 0.3.2
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-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple and composable event library
14
14
  email: mat@blocknot.es
@@ -20,6 +20,7 @@ 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
25
26
  - lib/eventish/simple_event.rb