eventish 0.4.1 → 0.5.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: 04c5125d8999ca2ad1a5ed1a81f7eae2adc4380aa7402cf93c17c8d92e7879f4
4
- data.tar.gz: ef364bf047a9c29733793bb3d1b737842f392d414b61430f624d991700d9b560
3
+ metadata.gz: 419b6aafd21ffe1e4c9041549dc80b77fe7973975732a44feeac183f28d8f474
4
+ data.tar.gz: 50e7f43233ee05c86c930807677cdee82a21471b6d4f4c4a037ea1062e6f3127
5
5
  SHA512:
6
- metadata.gz: f39ee5f5a27fc61ae32a3a7c8747430334a437de11671b8ab6666ad3bbbb6636784d2b1b0a84a249ba5495408f24f75ccae70e350d20c76c7ddf8c77dc685eee
7
- data.tar.gz: 6dbbd5ff2495ed5a85263bbbdafe863fba988494426acfcaf142c122084effe60fbbbd557ec61cf780d9c2f2e1c839b6a2183159756093b7e72308f33d1fc84b
6
+ metadata.gz: b768c6c9baaac52a7b3bd841569714727a43527d90956881645e17b11afc5be65e14bfec6424d4a877f12d7294dc4a69edaf94a55b62b4da7f9bb133d1d69c41
7
+ data.tar.gz: a272f0d0420ae08b627be7fcb975d7051af6f7662b2089f91b4719a1b97fab70fd3cc7872031fba83ccb8d61881953f08d921476b4918cd9dfff82be05d6272b
data/README.md CHANGED
@@ -1,21 +1,22 @@
1
1
  # Eventish
2
2
 
3
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)
4
+ [![Specs Rails 6](https://github.com/blocknotes/eventish/actions/workflows/rails6.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/rails6.yml)
5
+ [![Specs Rails 7](https://github.com/blocknotes/eventish/actions/workflows/rails7.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/rails7.yml)
5
6
  [![Linters](https://github.com/blocknotes/eventish/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/eventish/actions/workflows/linters.yml)
6
7
 
7
8
  Yet another events library with a _simple_ API to handle... events :tada:
8
9
 
9
10
  Main features:
10
11
  - _composable_: just require the components that you need;
11
- - with [adapters](#adapters): support ActiveSupport::Notifications for pub/sub events;
12
+ - with [adapters](#adapters): support ActiveSupport::Notifications and Wisper for pub/sub events;
12
13
  - with [async events](#async-events): support ActiveJob for events background execution;
13
14
  - with [callbacks wrapper](#callbacks): support ActiveRecord callbacks.
14
15
  - with [plugins](#plugins): a simple logger and a Rails logger are included.
15
16
 
16
17
  Please :star: if you like it.
17
18
 
18
- > You need _eventish_ if you want to speak by events :smile:
19
+ > Do you want to speak by events? Try _eventish_ :smile:
19
20
 
20
21
  ## Install
21
22
 
@@ -60,8 +61,9 @@ For a complete example please take a look at the [dummy app](spec/dummy) in the
60
61
 
61
62
  ### Adapters
62
63
 
63
- The component used events subscription and publishing.
64
- Only _ActiveSupport Notification_ is supported for now.
64
+ Used for events subscription and publishing.
65
+
66
+ _ActiveSupport Notification_:
65
67
 
66
68
  ```rb
67
69
  # initializer setup
@@ -72,6 +74,18 @@ Eventish.setup do |config|
72
74
  end
73
75
  ```
74
76
 
77
+ _Wisper_ (NOTE: around callback wrappers are not supported):
78
+
79
+ ```rb
80
+ # initializer setup
81
+ require 'wisper'
82
+ require 'eventish/adapters/wisper'
83
+
84
+ Eventish.setup do |config|
85
+ config.adapter = Eventish::Adapters::Wisper
86
+ end
87
+ ```
88
+
75
89
  ### Simple events
76
90
 
77
91
  Generic events not related to a specific component.
@@ -117,6 +131,7 @@ Only _ActiveJob_ is supported for now.
117
131
 
118
132
  ```rb
119
133
  # initializer setup
134
+ require 'active_job/railtie'
120
135
  require 'eventish/active_job_event'
121
136
 
122
137
  Rails.configuration.after_initialize do
@@ -4,103 +4,126 @@ module Eventish
4
4
  module ActiveRecord
5
5
  module Callback
6
6
  # Init events
7
- def after_initialize_event(event)
8
- after_initialize -> { ::Eventish.publish(event, self) }
7
+ def after_initialize_event(*args)
8
+ event = args.shift
9
+ after_initialize -> { ::Eventish.publish(event, self) }, *args
9
10
  end
10
11
 
11
- def after_find_event(event)
12
- after_find -> { ::Eventish.publish(event, self) }
12
+ def after_find_event(*args)
13
+ event = args.shift
14
+ after_find -> { ::Eventish.publish(event, self) }, *args
13
15
  end
14
16
 
15
17
  # Validation events
16
- def before_validation_event(event)
17
- before_validation -> { ::Eventish.publish(event, self) }
18
+ def before_validation_event(*args)
19
+ event = args.shift
20
+ before_validation -> { ::Eventish.publish(event, self) }, *args
18
21
  end
19
22
 
20
- def after_validation_event(event)
21
- after_validation -> { ::Eventish.publish(event, self) }
23
+ def after_validation_event(*args)
24
+ event = args.shift
25
+ after_validation -> { ::Eventish.publish(event, self) }, *args
22
26
  end
23
27
 
24
28
  # Create events
25
- def before_create_event(event)
26
- before_create -> { ::Eventish.publish(event, self) }
29
+ def before_create_event(*args)
30
+ event = args.shift
31
+ before_create -> { ::Eventish.publish(event, self) }, *args
27
32
  end
28
33
 
29
- def around_create_event(event)
30
- around_create ->(_object, block) { ::Eventish.publish(event, self, block: block) }
34
+ def around_create_event(*args)
35
+ event = args.shift
36
+ around_create ->(_object, block) { ::Eventish.publish(event, self, block: block) }, *args
31
37
  end
32
38
 
33
- def after_create_event(event)
34
- after_create -> { ::Eventish.publish(event, self) }
39
+ def after_create_event(*args)
40
+ event = args.shift
41
+ after_create -> { ::Eventish.publish(event, self) }, *args
35
42
  end
36
43
 
37
44
  # Update events
38
- def before_update_event(event)
39
- before_update -> { ::Eventish.publish(event, self) }
45
+ def before_update_event(*args)
46
+ event = args.shift
47
+ before_update -> { ::Eventish.publish(event, self) }, *args
40
48
  end
41
49
 
42
- def around_update_event(event)
43
- around_update ->(_object, block) { ::Eventish.publish(event, self, block: block) }
50
+ def around_update_event(*args)
51
+ event = args.shift
52
+ around_update ->(_object, block) { ::Eventish.publish(event, self, block: block) }, *args
44
53
  end
45
54
 
46
- def after_update_event(event)
47
- after_update -> { ::Eventish.publish(event, self) }
55
+ def after_update_event(*args)
56
+ event = args.shift
57
+ after_update -> { ::Eventish.publish(event, self) }, *args
48
58
  end
49
59
 
50
60
  # Save events
51
- def before_save_event(event)
52
- before_save -> { ::Eventish.publish(event, self) }
61
+ def before_save_event(*args)
62
+ event = args.shift
63
+ before_save -> { ::Eventish.publish(event, self) }, *args
53
64
  end
54
65
 
55
- def around_save_event(event)
56
- around_save ->(_object, block) { ::Eventish.publish(event, self, block: block) }
66
+ def around_save_event(*args)
67
+ event = args.shift
68
+ around_save ->(_object, block) { ::Eventish.publish(event, self, block: block) }, *args
57
69
  end
58
70
 
59
- def after_save_event(event)
60
- after_save -> { ::Eventish.publish(event, self) }
71
+ def after_save_event(*args)
72
+ event = args.shift
73
+ after_save -> { ::Eventish.publish(event, self) }, *args
61
74
  end
62
75
 
63
76
  # Destroy events
64
- def before_destroy_event(event)
65
- before_destroy -> { ::Eventish.publish(event, self) }
77
+ def before_destroy_event(*args)
78
+ event = args.shift
79
+ before_destroy -> { ::Eventish.publish(event, self) }, *args
66
80
  end
67
81
 
68
- def around_destroy_event(event)
69
- around_destroy ->(_object, block) { ::Eventish.publish(event, self, block: block) }
82
+ def around_destroy_event(*args)
83
+ event = args.shift
84
+ around_destroy ->(_object, block) { ::Eventish.publish(event, self, block: block) }, *args
70
85
  end
71
86
 
72
- def after_destroy_event(event)
73
- after_destroy -> { ::Eventish.publish(event, self) }
87
+ def after_destroy_event(*args)
88
+ event = args.shift
89
+ after_destroy -> { ::Eventish.publish(event, self) }, *args
74
90
  end
75
91
 
76
92
  # Commit events
77
- def after_commit_event(event)
78
- after_commit -> { ::Eventish.publish(event, self) }
93
+ def after_commit_event(*args)
94
+ event = args.shift
95
+ after_commit -> { ::Eventish.publish(event, self) }, *args
79
96
  end
80
97
 
81
- def after_create_commit_event(event)
82
- after_create_commit -> { ::Eventish.publish(event, self) }
98
+ def after_create_commit_event(*args)
99
+ event = args.shift
100
+ after_create_commit -> { ::Eventish.publish(event, self) }, *args
83
101
  end
84
102
 
85
- def after_update_commit_event(event)
86
- after_update_commit -> { ::Eventish.publish(event, self) }
103
+ def after_update_commit_event(*args)
104
+ event = args.shift
105
+ after_update_commit -> { ::Eventish.publish(event, self) }, *args
87
106
  end
88
107
 
89
- def after_save_commit_event(event)
90
- after_save_commit -> { ::Eventish.publish(event, self) }
108
+ def after_save_commit_event(*args)
109
+ event = args.shift
110
+ after_save_commit -> { ::Eventish.publish(event, self) }, *args
91
111
  end
92
112
 
93
- def after_destroy_commit_event(event)
94
- after_destroy_commit -> { ::Eventish.publish(event, self) }
113
+ def after_destroy_commit_event(*args)
114
+ event = args.shift
115
+ after_destroy_commit -> { ::Eventish.publish(event, self) }, *args
95
116
  end
96
117
 
97
- def after_rollback_event(event)
98
- after_rollback -> { ::Eventish.publish(event, self) }
118
+ def after_rollback_event(*args)
119
+ event = args.shift
120
+ after_rollback -> { ::Eventish.publish(event, self) }, *args
99
121
  end
100
122
 
101
123
  # Touch events
102
- def after_touch_event(event)
103
- after_touch -> { ::Eventish.publish(event, self) }
124
+ def after_touch_event(*args)
125
+ event = args.shift
126
+ after_touch -> { ::Eventish.publish(event, self) }, *args
104
127
  end
105
128
  end
106
129
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eventish
4
- class Adapters
4
+ module Adapters
5
5
  class ActiveSupport
6
6
  class << self
7
7
  def publish(event, target = nil, block: nil)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eventish
4
+ module Adapters
5
+ class Wisper
6
+ class << self
7
+ include ::Wisper::Publisher
8
+
9
+ def publish(event, target = nil, block: nil)
10
+ raise ArgumentError, 'Missing event to publish' if event.nil?
11
+
12
+ options = block ? { block: block } : {} # TODO: verify block feature
13
+ broadcast(event.to_s, target, options)
14
+ end
15
+
16
+ def subscribe(event, handler)
17
+ raise ArgumentError, 'Missing event to subscribe' if event.nil?
18
+ raise ArgumentError, 'Missing handler for subscription' if handler.nil?
19
+
20
+ ::Wisper.subscribe(handler.new, on: event.to_s, with: :call).tap do |subscriber|
21
+ Eventish.subscribers[event.to_s] = subscriber
22
+ end
23
+ end
24
+
25
+ def unsubscribe(event)
26
+ raise ArgumentError, 'Missing event to unsubscribe' if event.nil?
27
+
28
+ subscriber = Eventish.subscribers[event.to_s]
29
+ ::Wisper.unsubscribe(subscriber)
30
+ Eventish.subscribers.delete(event.to_s)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -27,10 +27,9 @@ module Eventish
27
27
  end
28
28
 
29
29
  def subscribe_all
30
- # iterate the descendants
31
30
  ignore_events = [Eventish::SimpleEvent]
32
31
  ignore_events.push(Eventish::ActiveJobEvent) if defined? Eventish::ActiveJobEvent
33
- events = ObjectSpace.each_object(singleton_class).sort
32
+ events = Eventish.descendants(self).sort
34
33
  (events - ignore_events).each(&:subscribe)
35
34
  end
36
35
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eventish # :nodoc:
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/eventish.rb CHANGED
@@ -18,6 +18,17 @@ module Eventish
18
18
  @options ||= Struct.new(*OPTIONS).new # rubocop:disable Naming/MemoizedInstanceVariableName
19
19
  end
20
20
 
21
+ # From Rails 6.0
22
+ def descendants(klass)
23
+ descendants = []
24
+ ObjectSpace.each_object(klass.singleton_class) do |k|
25
+ next if k.singleton_class?
26
+
27
+ descendants.unshift k unless k == self
28
+ end
29
+ descendants
30
+ end
31
+
21
32
  def publish(event_name, target = nil, block: nil)
22
33
  config.adapter&.publish(event_name, target, block: block)
23
34
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.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-24 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appraisal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
13
27
  description: A simple and composable event library
14
28
  email: mat@blocknot.es
15
29
  executables: []
@@ -22,6 +36,7 @@ files:
22
36
  - lib/eventish/active_job_event.rb
23
37
  - lib/eventish/active_record/callback.rb
24
38
  - lib/eventish/adapters/active_support.rb
39
+ - lib/eventish/adapters/wisper.rb
25
40
  - lib/eventish/event_api.rb
26
41
  - lib/eventish/plugins/logger.rb
27
42
  - lib/eventish/plugins/rails_logger.rb