use_cases 1.1.1 → 1.1.4

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: c61b0fc85c1ed9ffdee1bc0f03b6991a7fcd9b64bb502e5b6859e5e124865068
4
- data.tar.gz: b99383f71920f2c376e2bb197e4a7be3140630b36dcd33b3169bc15582adc5ac
3
+ metadata.gz: 34892c434db7409635d2628aee340dc76f71f2386c3b28c591b958d3fd7b9581
4
+ data.tar.gz: 922c959f22bbf32c82de1913a6384d46058eec701274dcaaee8dd5cb63fc09df
5
5
  SHA512:
6
- metadata.gz: 93f9d4b5b4f7492ac49d142b51336b6837c3ad8ff54706a1fa363bd851bd6f7d366e6131bb5e8399ef32de8e38608c07f33c8794ab7cb5fd571e6450b8f866f6
7
- data.tar.gz: ea72716e2909d65ec10904df231262ba6104cab8e564eb99129f1ba2adfaf2a43b1e93b5ab7c1285aa3e72f76d52b9440e65ce2c7a5a903ae191685f4bee3a39
6
+ metadata.gz: 8d2da877b7f1e0ad6e9cc01a560126110b8e53e9e1364b1cea07943318380c766499890b0bec5b67d8c3687742489f902cade0f76761cf6da693942cf01cc9c7
7
+ data.tar.gz: db808f18bb859f6f9faa2ac6aac8c6f5dcee9e820235e35b74bf5074fe262f7b477e2aa344c9d14d815b6739c2b47f7b530f3fdb8c84bf61c8f6407e41c6e237
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: 'bundler'
4
+ directory: '/'
5
+ schedule:
6
+ interval: 'monthly'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- use_cases (1.1.1)
4
+ use_cases (1.1.3)
5
5
  activesupport
6
6
  dry-events
7
7
  dry-matcher
data/README.md CHANGED
@@ -68,7 +68,7 @@ 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 |
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
72
  | `:transactional` | Calls `#transaction` on a given `transaction_handler` object around the use case execution |
73
73
  | `:validated` | Adds all methods of `dry-transaction` to the use case DSL, which run validations on the received `params` object. |
74
74
 
@@ -122,7 +122,7 @@ class Users::DeleteAccount
122
122
 
123
123
  authorize :user_owns_account?, failure_message: 'Cannot delete account'
124
124
  try :load_account, catch: ActiveRecord::RecordNotFound, failure: :account_not_found, failure_message: 'Account not found'
125
- map :delete_account, publish: :account_deleted
125
+ map :delete_account, publish: :account_deleted, event_data: proc { |account| { account_id: account.id } }, event_metadata: proc { { published_by: 'users.delete_account' } }
126
126
  enqueue :send_farewell_email
127
127
 
128
128
  private
@@ -16,6 +16,8 @@ module UseCases
16
16
  module StepPatch
17
17
  def initialize(*)
18
18
  super
19
+ return unless options[:publish]
20
+
19
21
  %w[success failure].map do |event_type|
20
22
  event_id = [options[:publish], event_type].join(".")
21
23
  Events::EventRegistry.register(event_id)
@@ -40,10 +42,10 @@ module UseCases
40
42
  private
41
43
 
42
44
  def extract_payload(step_result, args)
45
+ value = step_result.value!
43
46
  {
44
- return_value: step_result.value!,
45
- params: args[-2],
46
- current_user: args[-1]
47
+ data: options[:event_data]&.call(value, args) || {},
48
+ metadata: options[:event_metadata]&.call(value, args) || {}
47
49
  }
48
50
  end
49
51
 
@@ -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.to_h])
79
+ Failure([:validation_error, UseCases.transform_validation_errors.(validation.errors)])
73
80
  end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UseCases
4
- VERSION = "1.1.1"
4
+ VERSION = "1.1.4"
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,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.1
4
+ version: 1.1.4
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-14 00:00:00.000000000 Z
11
+ date: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -143,6 +143,7 @@ executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
+ - ".github/dependabot.yml"
146
147
  - ".github/workflows/main.yml"
147
148
  - ".gitignore"
148
149
  - ".rspec"