use_cases 1.1.3 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +6 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +5 -6
- data/README.md +4 -5
- data/lib/use_case.rb +0 -1
- data/lib/use_cases/module_optins.rb +0 -2
- data/lib/use_cases/version.rb +1 -1
- data/lib/use_cases.rb +0 -1
- data/use_cases.gemspec +5 -6
- metadata +12 -30
- data/lib/use_cases/events/event_registry.rb +0 -23
- data/lib/use_cases/events/publish_job.rb +0 -15
- data/lib/use_cases/events/publisher.rb +0 -25
- data/lib/use_cases/events/subscriber.rb +0 -17
- data/lib/use_cases/module_optins/publishing.rb +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92d41d9c7f9da2600b22ffcad2013a9115f39f42ef41df49b2c592a18fb4115b
|
4
|
+
data.tar.gz: c0a3f6f88235e144107ba141b3e8add9bcca2ee42dce3f2f2e0ea0562ef114a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 251c8400f48af05b11ea871a2750531071548050725f341aabd8bfb4a1bca8b68a1fd6d69eb1db3b6af93da888f89e37bd5985c6933a3f541ac63650fccf8ba1
|
7
|
+
data.tar.gz: 65e556b50fe0cd77bef9c0d0e4c9652c88434a53b616a46677e352df7a4469e6b6c8959274859989ed821f305c3a90342476302df7e60971dbc2708b4c40e300
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
use_cases (
|
5
|
-
activesupport
|
6
|
-
dry-
|
7
|
-
dry-
|
8
|
-
dry-
|
9
|
-
dry-validation
|
4
|
+
use_cases (2.0.2)
|
5
|
+
activesupport (>= 5.1)
|
6
|
+
dry-matcher (>= 0.8.1)
|
7
|
+
dry-monads (>= 1.0.0)
|
8
|
+
dry-validation (>= 0.11.0)
|
10
9
|
|
11
10
|
GEM
|
12
11
|
remote: https://rubygems.org/
|
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
|
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` option 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, :
|
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, :
|
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
|
124
|
+
map :delete_account
|
126
125
|
enqueue :send_farewell_email
|
127
126
|
|
128
127
|
private
|
data/lib/use_case.rb
CHANGED
@@ -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)
|
data/lib/use_cases/version.rb
CHANGED
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
|
data/use_cases.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = "A DSL to encapsulate your domain logic."
|
13
13
|
spec.homepage = "https://github.com/listminut/use_cases"
|
14
14
|
spec.license = "MIT"
|
15
|
-
spec.required_ruby_version = ">= 2.6.
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
16
|
|
17
17
|
spec.metadata["homepage_uri"] = spec.homepage
|
18
18
|
spec.metadata["source_code_uri"] = "https://github.com/listminut/use_cases"
|
@@ -30,11 +30,10 @@ Gem::Specification.new do |spec|
|
|
30
30
|
# For more information and examples about making a new gem, checkout our
|
31
31
|
# guide at: https://bundler.io/guides/creating_gem.html
|
32
32
|
|
33
|
-
spec.add_dependency "activesupport"
|
34
|
-
spec.add_dependency "dry-
|
35
|
-
spec.add_dependency "dry-
|
36
|
-
spec.add_dependency "dry-
|
37
|
-
spec.add_dependency "dry-validation"
|
33
|
+
spec.add_dependency "activesupport", ">= 5.1"
|
34
|
+
spec.add_dependency "dry-matcher", ">= 0.8.1"
|
35
|
+
spec.add_dependency "dry-monads", ">= 1.0.0"
|
36
|
+
spec.add_dependency "dry-validation", ">= 0.11.0"
|
38
37
|
|
39
38
|
spec.add_development_dependency "byebug"
|
40
39
|
spec.add_development_dependency "rake"
|
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:
|
4
|
+
version: 2.0.2
|
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-
|
11
|
+
date: 2022-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,70 +16,56 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: dry-events
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
26
|
+
version: '5.1'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: dry-matcher
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
31
|
- - ">="
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
33
|
+
version: 0.8.1
|
48
34
|
type: :runtime
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
40
|
+
version: 0.8.1
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: dry-monads
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - ">="
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
47
|
+
version: 1.0.0
|
62
48
|
type: :runtime
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
52
|
- - ">="
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
54
|
+
version: 1.0.0
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: dry-validation
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
59
|
- - ">="
|
74
60
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
61
|
+
version: 0.11.0
|
76
62
|
type: :runtime
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
66
|
- - ">="
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
68
|
+
version: 0.11.0
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: byebug
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,6 +129,7 @@ executables: []
|
|
143
129
|
extensions: []
|
144
130
|
extra_rdoc_files: []
|
145
131
|
files:
|
132
|
+
- ".github/dependabot.yml"
|
146
133
|
- ".github/workflows/main.yml"
|
147
134
|
- ".gitignore"
|
148
135
|
- ".rspec"
|
@@ -160,14 +147,9 @@ files:
|
|
160
147
|
- lib/use_case.rb
|
161
148
|
- lib/use_cases.rb
|
162
149
|
- lib/use_cases/dsl.rb
|
163
|
-
- lib/use_cases/events/event_registry.rb
|
164
|
-
- lib/use_cases/events/publish_job.rb
|
165
|
-
- lib/use_cases/events/publisher.rb
|
166
|
-
- lib/use_cases/events/subscriber.rb
|
167
150
|
- lib/use_cases/module_optins.rb
|
168
151
|
- lib/use_cases/module_optins/authorized.rb
|
169
152
|
- lib/use_cases/module_optins/prepared.rb
|
170
|
-
- lib/use_cases/module_optins/publishing.rb
|
171
153
|
- lib/use_cases/module_optins/transactional.rb
|
172
154
|
- lib/use_cases/module_optins/validated.rb
|
173
155
|
- lib/use_cases/params.rb
|
@@ -201,7 +183,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
201
183
|
requirements:
|
202
184
|
- - ">="
|
203
185
|
- !ruby/object:Gem::Version
|
204
|
-
version: 2.6.
|
186
|
+
version: 2.6.0
|
205
187
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
206
188
|
requirements:
|
207
189
|
- - ">="
|
@@ -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
|
-
{
|
46
|
-
return_value: step_result.value!,
|
47
|
-
params: args[-2],
|
48
|
-
current_user: args[-1]
|
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
|