use_cases 1.1.0 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/use_cases/events/event_registry.rb +23 -0
- data/lib/use_cases/events/publish_job.rb +5 -3
- data/lib/use_cases/events/publisher.rb +3 -1
- data/lib/use_cases/events/subscriber.rb +4 -2
- data/lib/use_cases/module_optins/authorized.rb +2 -2
- data/lib/use_cases/module_optins/publishing.rb +7 -21
- data/lib/use_cases/module_optins/validated.rb +16 -7
- data/lib/use_cases/module_optins.rb +1 -1
- data/lib/use_cases/rspec/matchers.rb +1 -1
- data/lib/use_cases/version.rb +1 -1
- data/lib/use_cases.rb +11 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '040658ce42b3542dc655d8102ebbaae334243a3e56a3854810c29b55e24e442d'
|
4
|
+
data.tar.gz: 9e9087416c8d4805dbb55d0fcff1f59628d8ec18b564099004bde75dbaa80142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f841052453fd910636c9a3c3fb073349cba385dc6faebc8af42b5c7dfce6ecf2202d29491b714590ce6d2f03a90b6940ca348ce1648b69c67db5ad92fc1a5c5
|
7
|
+
data.tar.gz: fb9e3a7b3b6e91e76121281519fade25f2e7cee12801696b5197e98b14630c1fe7ffac8c240a0de1aba5e290668631587cfa57e2297369454f5e0a4333e755e9
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,23 @@
|
|
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,12 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
return unless defined? ActiveJob
|
3
4
|
|
4
5
|
module UseCases
|
5
6
|
module Events
|
6
7
|
class PublishJob < ActiveJob::Base
|
7
|
-
def perform(
|
8
|
-
|
9
|
-
|
8
|
+
def perform(event_id, payload)
|
9
|
+
event_id += ".async"
|
10
|
+
Events::EventRegistry.register(event_id)
|
11
|
+
UseCases.publisher.publish(event_id, payload)
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "dry/events/publisher"
|
2
4
|
|
3
5
|
module UseCases
|
@@ -16,7 +18,7 @@ module UseCases
|
|
16
18
|
|
17
19
|
def publish(event_name, payload)
|
18
20
|
super
|
19
|
-
PublishJob.perform_later(event_name, payload) if defined?(PublishJob)
|
21
|
+
PublishJob.perform_later(event_name, payload) if defined?(PublishJob) && !event_name.to_s.end_with?(".async")
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module UseCases
|
2
4
|
module Events
|
3
5
|
module Subscriber
|
@@ -6,8 +8,8 @@ module UseCases
|
|
6
8
|
end
|
7
9
|
|
8
10
|
def should_subscribe_to?(event_name)
|
9
|
-
event_handler_name = "on_#{event_name.gsub(
|
10
|
-
|
11
|
+
event_handler_name = "on_#{event_name.gsub(".", "_")}"
|
12
|
+
|
11
13
|
respond_to?(event_handler_name.to_sym)
|
12
14
|
end
|
13
15
|
end
|
@@ -17,10 +17,10 @@ module UseCases
|
|
17
17
|
failure: :unauthorized,
|
18
18
|
failure_message: "Not Authorized",
|
19
19
|
merge_input_as: :resource
|
20
|
-
}
|
20
|
+
}.freeze
|
21
21
|
|
22
22
|
def authorize(name, options = {})
|
23
|
-
options = DEFAULT_OPTIONS.merge(options)
|
23
|
+
options = DEFAULT_OPTIONS.merge(options)
|
24
24
|
|
25
25
|
check name, options
|
26
26
|
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
require "use_cases/events/publisher"
|
4
4
|
require "use_cases/events/subscriber"
|
5
5
|
require "use_cases/events/publish_job"
|
6
|
+
require "use_cases/events/event_registry"
|
6
7
|
|
7
8
|
module UseCases
|
8
9
|
module ModuleOptins
|
@@ -15,7 +16,12 @@ module UseCases
|
|
15
16
|
module StepPatch
|
16
17
|
def initialize(*)
|
17
18
|
super
|
18
|
-
|
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
|
19
25
|
end
|
20
26
|
|
21
27
|
def call(*args)
|
@@ -33,26 +39,6 @@ module UseCases
|
|
33
39
|
UseCases.publisher.publish(key, payload)
|
34
40
|
end
|
35
41
|
|
36
|
-
|
37
|
-
def finalize_subscriptions!
|
38
|
-
event_ids.each do |event_id|
|
39
|
-
listener_name = ['on', event_id].join('_').gsub('.', '_')
|
40
|
-
UseCases.publisher.register_event(event_id)
|
41
|
-
|
42
|
-
UseCases.subscribers.each do |subscriber|
|
43
|
-
next if !subscriber.respond_to?(listener_name) || UseCases.publisher.subscribed?(subscriber.method(listener_name))
|
44
|
-
|
45
|
-
UseCases.publisher.subscribe(subscriber)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def event_ids
|
51
|
-
%w[success failure success.async failure.async].map do |event_type|
|
52
|
-
[options[:publish], event_type].join('.')
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
42
|
private
|
57
43
|
|
58
44
|
def extract_payload(step_result, args)
|
@@ -32,29 +32,36 @@ module UseCases
|
|
32
32
|
def params(*args, &blk)
|
33
33
|
_setup_validation
|
34
34
|
|
35
|
-
_contract_class.params(*args, &blk)
|
35
|
+
_contract_class.params(*args, &block_with_config(&blk))
|
36
36
|
end
|
37
37
|
|
38
38
|
def schema(*args, &blk)
|
39
39
|
_setup_validation
|
40
40
|
|
41
|
-
_contract_class.schema(*args, &blk)
|
41
|
+
_contract_class.schema(*args, &block_with_config(&blk))
|
42
42
|
end
|
43
43
|
|
44
44
|
def rule(*args, &blk)
|
45
45
|
_setup_validation
|
46
46
|
|
47
|
-
_contract_class.rule(*args, &blk)
|
47
|
+
_contract_class.rule(*args, &block_with_config(&blk))
|
48
48
|
end
|
49
49
|
|
50
50
|
def json(*args, &blk)
|
51
51
|
_setup_validation
|
52
52
|
|
53
|
-
_contract_class.json(*args, &blk)
|
53
|
+
_contract_class.json(*args, &block_with_config(&blk))
|
54
54
|
end
|
55
55
|
|
56
56
|
def option(*args, &blk)
|
57
|
-
_contract_class.option(*args, &blk)
|
57
|
+
_contract_class.option(*args, &block_with_config(&blk))
|
58
|
+
end
|
59
|
+
|
60
|
+
def block_with_config(&blk)
|
61
|
+
Proc.new do
|
62
|
+
instance_exec(&UseCases.dry_validation)
|
63
|
+
instance_exec(&blk)
|
64
|
+
end
|
58
65
|
end
|
59
66
|
end
|
60
67
|
|
@@ -69,12 +76,14 @@ module UseCases
|
|
69
76
|
params.merge!(validation.to_h)
|
70
77
|
Success(validation.to_h)
|
71
78
|
else
|
72
|
-
Failure([:validation_error, validation.errors
|
79
|
+
Failure([:validation_error, UseCases.transform_validation_errors.(validation.errors)])
|
73
80
|
end
|
74
81
|
end
|
75
82
|
|
76
83
|
def contract
|
77
|
-
return
|
84
|
+
return unless self.class._contract_class_defined?
|
85
|
+
|
86
|
+
self.class._contract_class.new
|
78
87
|
end
|
79
88
|
|
80
89
|
module ClassMethods
|
@@ -29,7 +29,7 @@ RSpec::Matchers.define(:be_failure_with_payload) do |expected_result|
|
|
29
29
|
else
|
30
30
|
"the use case was expected to fail but it succeeded with #{test_subject.success.inspect}"
|
31
31
|
end
|
32
|
-
end
|
32
|
+
end
|
33
33
|
end
|
34
34
|
|
35
35
|
RSpec::Matchers.define(:be_failure_with) do |*expected_failure|
|
data/lib/use_cases/version.rb
CHANGED
data/lib/use_cases.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "dry/configurable"
|
4
|
+
require "dry/types"
|
4
5
|
|
5
6
|
require_relative "use_case"
|
6
7
|
require_relative "use_cases/version"
|
@@ -17,4 +18,14 @@ module UseCases
|
|
17
18
|
setting :container, reader: true
|
18
19
|
setting :publisher, default: ::UseCases::Events::Publisher.new, reader: true
|
19
20
|
setting :subscribers, default: [], reader: true
|
21
|
+
setting :dry_validation, default: ->(config) {}
|
22
|
+
setting :transform_validation_errors, default: ->(errors) { errors.to_h }, reader: true
|
23
|
+
|
24
|
+
def self.dry_validation
|
25
|
+
config_proc = config.dry_validation
|
26
|
+
|
27
|
+
Proc.new do
|
28
|
+
config_proc.call(config)
|
29
|
+
end
|
30
|
+
end
|
20
31
|
end
|
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
|
+
version: 1.1.3
|
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-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -160,6 +160,7 @@ files:
|
|
160
160
|
- lib/use_case.rb
|
161
161
|
- lib/use_cases.rb
|
162
162
|
- lib/use_cases/dsl.rb
|
163
|
+
- lib/use_cases/events/event_registry.rb
|
163
164
|
- lib/use_cases/events/publish_job.rb
|
164
165
|
- lib/use_cases/events/publisher.rb
|
165
166
|
- lib/use_cases/events/subscriber.rb
|