use_cases 1.1.4 → 2.0.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: 34892c434db7409635d2628aee340dc76f71f2386c3b28c591b958d3fd7b9581
4
- data.tar.gz: 922c959f22bbf32c82de1913a6384d46058eec701274dcaaee8dd5cb63fc09df
3
+ metadata.gz: c3434ac25b743c9794d09774344e6e2af73e4b2077a2d7d0aef31628ffc4a75d
4
+ data.tar.gz: de20ba12bb9a7ac43cf6bdb8ac35c96973de8b63f8cd90fee4b016e2e3459827
5
5
  SHA512:
6
- metadata.gz: 8d2da877b7f1e0ad6e9cc01a560126110b8e53e9e1364b1cea07943318380c766499890b0bec5b67d8c3687742489f902cade0f76761cf6da693942cf01cc9c7
7
- data.tar.gz: db808f18bb859f6f9faa2ac6aac8c6f5dcee9e820235e35b74bf5074fe262f7b477e2aa344c9d14d815b6739c2b47f7b530f3fdb8c84bf61c8f6407e41c6e237
6
+ metadata.gz: 8755ef9b16ab142c6fa1a2156f0c539d366195af5f065aa4b182dfa539fd973cb5d1f800d26c43c886f36bd8fa6c91e71955b612df229d4577b117b1455515a1
7
+ data.tar.gz: 8c89294d590c86a6f6126e309ca4011839fb5c236269623493f319bd55424d1aef026010f3ae7723ff62d229deb9ecec83a0d94c7efdbfdce327b285486d698b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.0.0] - 2022-06-12
4
+
5
+ - Removes `publishing` module from the DSL.
6
+
3
7
  ## [1.0.1] - 2021-12-19
4
8
 
5
9
  - Async published events are now suffixed by `".async"`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- use_cases (1.1.3)
4
+ use_cases (2.0.0)
5
5
  activesupport
6
6
  dry-events
7
7
  dry-matcher
data/README.md CHANGED
@@ -22,7 +22,7 @@ In order to add optional modules (optins), use the following notation:
22
22
 
23
23
  ```ruby
24
24
  class Users::Create
25
- include UseCase[:validated, :transactional, :publishing]
25
+ include UseCase[:validated, :transactional]
26
26
  end
27
27
  ```
28
28
 
@@ -68,7 +68,6 @@ end
68
68
  |---|---|
69
69
  | `:authorized` | Adds an extra `authorize` step macro, used to check user permissions. |
70
70
  | `:prepared` | Adds an extra `prepare` step macro, used to run some code before the use case runs. |
71
- | `:publishing` | Adds extra extra `publish`, `event_data` and `event_metadata` options to all steps, which allows a step to broadcast an event after executing |
72
71
  | `:transactional` | Calls `#transaction` on a given `transaction_handler` object around the use case execution |
73
72
  | `:validated` | Adds all methods of `dry-transaction` to the use case DSL, which run validations on the received `params` object. |
74
73
 
@@ -105,7 +104,7 @@ Defining a step can be done in the body of the use case.
105
104
 
106
105
  ```ruby
107
106
  class Users::DeleteAccount
108
- include UseCases[:validated, :transactional, :publishing, :validated]
107
+ include UseCases[:validated, :transactional, :validated]
109
108
 
110
109
  step :do_something, {}
111
110
  ```
@@ -114,7 +113,7 @@ In real life, a simple use case would look something like:
114
113
 
115
114
  ```ruby
116
115
  class Users::DeleteAccount
117
- include UseCases[:validated, :transactional, :publishing, :validated]
116
+ include UseCases[:validated, :transactional, :validated]
118
117
 
119
118
  params do
120
119
  required(:id).filled(:str?)
@@ -122,7 +121,7 @@ class Users::DeleteAccount
122
121
 
123
122
  authorize :user_owns_account?, failure_message: 'Cannot delete account'
124
123
  try :load_account, catch: ActiveRecord::RecordNotFound, failure: :account_not_found, failure_message: 'Account not found'
125
- map :delete_account, publish: :account_deleted, event_data: proc { |account| { account_id: account.id } }, event_metadata: proc { { published_by: 'users.delete_account' } }
124
+ map :delete_account
126
125
  enqueue :send_farewell_email
127
126
 
128
127
  private
@@ -3,7 +3,6 @@
3
3
  require "use_cases/module_optins/prepared"
4
4
  require "use_cases/module_optins/transactional"
5
5
  require "use_cases/module_optins/validated"
6
- require "use_cases/module_optins/publishing"
7
6
  require "use_cases/module_optins/authorized"
8
7
 
9
8
  module UseCases
@@ -15,7 +14,6 @@ module UseCases
15
14
  transactional: Transactional,
16
15
  validated: Validated,
17
16
  prepared: Prepared,
18
- publishing: Publishing
19
17
  }.freeze
20
18
 
21
19
  def [](*options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UseCases
4
- VERSION = "1.1.4"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/use_cases.rb CHANGED
@@ -16,7 +16,6 @@ module UseCases
16
16
  extend Dry::Configurable
17
17
 
18
18
  setting :container, reader: true
19
- setting :publisher, default: ::UseCases::Events::Publisher.new, reader: true
20
19
  setting :subscribers, default: [], reader: true
21
20
  setting :dry_validation, default: ->(config) {}
22
21
  setting :transform_validation_errors, default: ->(errors) { errors.to_h }, reader: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: use_cases
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ring Twice
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-06 00:00:00.000000000 Z
11
+ date: 2022-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -161,14 +161,9 @@ files:
161
161
  - lib/use_case.rb
162
162
  - lib/use_cases.rb
163
163
  - lib/use_cases/dsl.rb
164
- - lib/use_cases/events/event_registry.rb
165
- - lib/use_cases/events/publish_job.rb
166
- - lib/use_cases/events/publisher.rb
167
- - lib/use_cases/events/subscriber.rb
168
164
  - lib/use_cases/module_optins.rb
169
165
  - lib/use_cases/module_optins/authorized.rb
170
166
  - lib/use_cases/module_optins/prepared.rb
171
- - lib/use_cases/module_optins/publishing.rb
172
167
  - lib/use_cases/module_optins/transactional.rb
173
168
  - lib/use_cases/module_optins/validated.rb
174
169
  - lib/use_cases/params.rb
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UseCases
4
- module Events
5
- class EventRegistry
6
- def self.register(event_id)
7
- event_ids << event_id
8
- listener_name = ["on", event_id].join("_").gsub(".", "_")
9
- UseCases.publisher.register_event(event_id)
10
-
11
- UseCases.subscribers.each do |subscriber|
12
- next if !subscriber.respond_to?(listener_name) || UseCases.publisher.subscribed?(subscriber.method(listener_name))
13
-
14
- UseCases.publisher.subscribe(subscriber)
15
- end
16
- end
17
-
18
- def self.event_ids
19
- @event_ids ||= []
20
- end
21
- end
22
- end
23
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- return unless defined? ActiveJob
4
-
5
- module UseCases
6
- module Events
7
- class PublishJob < ActiveJob::Base
8
- def perform(event_id, payload)
9
- event_id += ".async"
10
- Events::EventRegistry.register(event_id)
11
- UseCases.publisher.publish(event_id, payload)
12
- end
13
- end
14
- end
15
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dry/events/publisher"
4
-
5
- module UseCases
6
- module Events
7
- class Publisher
8
- include Dry::Events::Publisher[:use_cases]
9
-
10
- # This is a hack waiting for this https://github.com/dry-rb/dry-events/pull/15 to be merged
11
- def subscribed?(listener)
12
- __bus__.listeners.values.any? do |value|
13
- value.any? do |block, _|
14
- block.owner == listener.owner && block.name == listener.name
15
- end
16
- end
17
- end
18
-
19
- def publish(event_name, payload)
20
- super
21
- PublishJob.perform_later(event_name, payload) if defined?(PublishJob) && !event_name.to_s.end_with?(".async")
22
- end
23
- end
24
- end
25
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UseCases
4
- module Events
5
- module Subscriber
6
- def self.included(base)
7
- UseCases.subscribers << base.new
8
- end
9
-
10
- def should_subscribe_to?(event_name)
11
- event_handler_name = "on_#{event_name.gsub(".", "_")}"
12
-
13
- respond_to?(event_handler_name.to_sym)
14
- end
15
- end
16
- end
17
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "use_cases/events/publisher"
4
- require "use_cases/events/subscriber"
5
- require "use_cases/events/publish_job"
6
- require "use_cases/events/event_registry"
7
-
8
- module UseCases
9
- module ModuleOptins
10
- module Publishing
11
- def self.included(base)
12
- super
13
- StepAdapters::Abstract.prepend StepPatch
14
- end
15
-
16
- module StepPatch
17
- def initialize(*)
18
- super
19
- return unless options[:publish]
20
-
21
- %w[success failure].map do |event_type|
22
- event_id = [options[:publish], event_type].join(".")
23
- Events::EventRegistry.register(event_id)
24
- end
25
- end
26
-
27
- def call(*args)
28
- super(*args).tap do |result|
29
- publish_step_result(result, args)
30
- end
31
- end
32
-
33
- def publish_step_result(step_result, args)
34
- return unless options[:publish]
35
-
36
- key = extract_event_key(step_result)
37
- payload = extract_payload(step_result, args)
38
-
39
- UseCases.publisher.publish(key, payload)
40
- end
41
-
42
- private
43
-
44
- def extract_payload(step_result, args)
45
- value = step_result.value!
46
- {
47
- data: options[:event_data]&.call(value, args) || {},
48
- metadata: options[:event_metadata]&.call(value, args) || {}
49
- }
50
- end
51
-
52
- def extract_event_key(step_result)
53
- options[:publish].to_s + (step_result.success? ? ".success" : ".failure")
54
- end
55
- end
56
- end
57
- end
58
- end