eventish 0.2.0 → 0.3.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: af7a6bf91a4b7cc8af46ea2520aeef223eedcce5cedceac04b7d91cc43333936
4
- data.tar.gz: 1ab3f8d60e00c982c3044cf0fc7bf7e53dd1435914238842724d6f2f211698ba
3
+ metadata.gz: 965405052bda0cb9903d4a447c91e5db89c8b98a868ded8428ee0399652680bd
4
+ data.tar.gz: 4608f89362bd42919071b404d428c8a44178d608a51ae0c0eb2aeb0b9a3d40ee
5
5
  SHA512:
6
- metadata.gz: dd0f4b9d8d6ec2705bd0d6679296705b5e3838730fc3fbcaa6657490dc6c108db80886cf2399220545b0b374c904da0bd1ac19beaac3e9c7af9a3efb5c625bc9
7
- data.tar.gz: e1eb63690fde2b62cbfc0d9c52bb1beea73e03205abffef5aa24de8097c4e935d6ee15c2ee67d19dc6acd46050ef11d8b85b11cbcbd5caa86e24f6199f7c5fe2
6
+ metadata.gz: 78839ea7b019d89e6587692faa575e345c9b72eae8355833cb374a5b7906cedd97ebcc74e4e61db1ecf9ea3c4764f5de7eeacac0a79bd6448c080f341ef33db3
7
+ data.tar.gz: 9b88eb7b02e330c68449cb7e978c8b40c6bc0a19cab874bd082fbf897ef61a9ec67ad3d2be866b3936d0370f777f158d423a414d3f469a4656facdfb8d201cb1
data/README.md CHANGED
@@ -88,8 +88,7 @@ module Main
88
88
 
89
89
  class << self
90
90
  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
91
+ # this is optional, if not set the class name to string will be used
93
92
  'some_event'
94
93
  end
95
94
  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.0'
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.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-17 00:00:00.000000000 Z
11
+ date: 2022-05-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple and composable event library
14
14
  email: mat@blocknot.es