use_cases 1.0.13 → 1.1.2

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: 3cec89e8a4f4ecdabae86c99156447d8b39ffd5de5e918130b21ccae18f045a7
4
- data.tar.gz: e36cd9587aab933815775ee1181360ccea4a24e58e82c3a4a073a99bb565ff31
3
+ metadata.gz: 1acf28555c44f6ddde193195d340f9ea4dc9d3ffce2ec9ad5302d626bc3fa07a
4
+ data.tar.gz: eebecd33d8f5f64e0fc73e1174e7e32b7252d88f1c2de8543297ec3fd78fbab4
5
5
  SHA512:
6
- metadata.gz: fdaa3ea0104e5bb38190c5c65f59e44569d38b08f0f4174e1c82f5f3b6f0698900a7001463dc3215007a2af2f4030616cc038ae2f2c78aba388b2cab90c520ad
7
- data.tar.gz: 0c9663e589c521677529bb5b8a4cbe87c4c3591c82dfbc30ea9248b215c53f16c915f4a37dd3683f22b8dc7698f20e46e7cc8dfe20c5390ae9daaf418ee8e26d
6
+ metadata.gz: 9e728db5f63082ce761579c57bf3b9ab22a2059d5720823bfda3c90105b60be10dc7e93ad7d0c87cef7b848bbc2ffd061a4cd806e386c030b34b39c04974ae12
7
+ data.tar.gz: 7f04086052168ba6af1933d2e196fbb4e5053188270b72f3ebf245007863be40695aa10d3dc98c7ea53845c3f88bebb68d35f5a316f39203b36e0906694623df
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- use_cases (1.0.12)
4
+ use_cases (1.1.1)
5
5
  activesupport
6
6
  dry-events
7
7
  dry-matcher
@@ -110,6 +110,7 @@ GEM
110
110
 
111
111
  PLATFORMS
112
112
  x86_64-darwin-20
113
+ x86_64-darwin-21
113
114
  x86_64-linux
114
115
 
115
116
  DEPENDENCIES
@@ -126,4 +127,4 @@ DEPENDENCIES
126
127
  use_cases!
127
128
 
128
129
  BUNDLED WITH
129
- 2.2.28
130
+ 2.2.32
@@ -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(publish_key, payload)
8
- publish_key += ".async"
9
- UseCases.publisher.subscribe_and_publish_event(publish_key, payload)
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
@@ -5,47 +7,18 @@ module UseCases
5
7
  class Publisher
6
8
  include Dry::Events::Publisher[:use_cases]
7
9
 
8
- def subscribe_and_publish_event(event_name, payload)
9
- subscribe_to_event(event_name)
10
- publish(event_name, payload)
11
- publish_async(event_name, payload) if event_should_be_published_asynchronously?(event_name)
12
- end
13
-
14
- private
15
-
16
- def publish_async(event_name, payload)
17
- UseCases::Events::PublishJob.perform_later(event_name, payload)
18
- end
19
-
20
- def event_should_be_published_asynchronously?(event_name)
21
- defined? UseCases::Events::PublishJob && !event_name.end_with?(".async")
22
- end
23
-
24
- def subscribe_to_event(event_name)
25
- subscribers_for(event_name).each(&method(:subscribe))
26
- end
27
-
28
- def subscribers_for(event_name)
29
- available_subscribers.select do |subscriber|
30
- subscriber.should_subscribe_to?(event_name) && !subscribed?(subscriber)
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
31
16
  end
32
17
  end
33
18
 
34
- def subscribe(listener)
35
- subcribers << listener
19
+ def publish(event_name, payload)
36
20
  super
37
- end
38
-
39
- def subscribed?(listener)
40
- subcribers.include?(listener)
41
- end
42
-
43
- def subcribers
44
- @subcribers ||= []
45
- end
46
-
47
- def available_subscribers
48
- UseCases.subscribers
21
+ PublishJob.perform_later(event_name, payload) if defined?(PublishJob) && !event_name.to_s.end_with?(".async")
49
22
  end
50
23
  end
51
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
@@ -13,29 +14,16 @@ module UseCases
13
14
  end
14
15
 
15
16
  module StepPatch
16
- def initialize(*args)
17
+ def initialize(*)
17
18
  super
18
- return unless options[:publish] && UseCases.publisher
19
-
20
- register_events
21
- end
22
-
23
- def register_events
24
- event_names.each do |event_name|
25
- next if UseCases.publisher.class.events[event_name]
19
+ return unless options[:publish]
26
20
 
27
- UseCases.publisher.class.register_event(event_name)
21
+ %w[success failure].map do |event_type|
22
+ event_id = [options[:publish], event_type].join(".")
23
+ Events::EventRegistry.register(event_id)
28
24
  end
29
25
  end
30
26
 
31
- def event_names
32
- event_name = options[:publish]
33
- [
34
- "#{event_name}.success", "#{event_name}.failure",
35
- "#{event_name}.success.async", "#{event_name}.failure.async"
36
- ]
37
- end
38
-
39
27
  def call(*args)
40
28
  super(*args).tap do |result|
41
29
  publish_step_result(result, args)
@@ -48,7 +36,7 @@ module UseCases
48
36
  key = extract_event_key(step_result)
49
37
  payload = extract_payload(step_result, args)
50
38
 
51
- UseCases.publisher.subscribe_and_publish_event(key, payload)
39
+ UseCases.publisher.publish(key, payload)
52
40
  end
53
41
 
54
42
  private
@@ -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
 
@@ -74,7 +81,9 @@ module UseCases
74
81
  end
75
82
 
76
83
  def contract
77
- return self.class._contract_class.new if self.class._contract_class_defined?
84
+ return unless self.class._contract_class_defined?
85
+
86
+ self.class._contract_class.new
78
87
  end
79
88
 
80
89
  module ClassMethods
@@ -16,7 +16,7 @@ module UseCases
16
16
  validated: Validated,
17
17
  prepared: Prepared,
18
18
  publishing: Publishing
19
- }
19
+ }.freeze
20
20
 
21
21
  def [](*options)
22
22
  modules = [self]
@@ -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|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UseCases
4
- VERSION = "1.0.13"
4
+ VERSION = "1.1.2"
5
5
  end
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,13 @@ 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
+
23
+ def self.dry_validation
24
+ config_proc = config.dry_validation
25
+
26
+ Proc.new do
27
+ config_proc.call(config)
28
+ end
29
+ end
20
30
  end
data/use_cases.gemspec CHANGED
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_dependency "dry-monads"
37
37
  spec.add_dependency "dry-validation"
38
38
 
39
+ spec.add_development_dependency "byebug"
39
40
  spec.add_development_dependency "rake"
40
41
  spec.add_development_dependency "rspec"
41
42
  spec.add_development_dependency "rubocop"
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.0.13
4
+ version: 1.1.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-02-05 00:00:00.000000000 Z
11
+ date: 2022-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -146,6 +160,7 @@ files:
146
160
  - lib/use_case.rb
147
161
  - lib/use_cases.rb
148
162
  - lib/use_cases/dsl.rb
163
+ - lib/use_cases/events/event_registry.rb
149
164
  - lib/use_cases/events/publish_job.rb
150
165
  - lib/use_cases/events/publisher.rb
151
166
  - lib/use_cases/events/subscriber.rb