use_cases 0.3.8 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "use_cases/step_adapters/tee"
4
-
5
- module UseCases
6
- module Prepare
7
- def self.included(base)
8
- base.class_eval do
9
- extend DSL
10
- end
11
- end
12
-
13
- module DSL
14
- def prepare(name, options = {})
15
- __steps__.unshift StepAdapters::Tee.new(name, nil, options)
16
- end
17
- end
18
- end
19
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "use_cases/step_adapters/check"
4
-
5
- module UseCases
6
- module StepAdapters
7
- class Authorize < UseCases::StepAdapters::Check
8
- class InvalidReturnValue < StandardError; end
9
-
10
- def do_call(*args)
11
- result = super(*args)
12
- prev_result = previous_step_result.value
13
- raise InvalidReturnValue, "The return value should not be a Monad." if result.is_a?(Dry::Monads::Result)
14
-
15
- failure_code = options[:failure] || :unauthorized
16
- failure_message = options[:failure_message] || "Not Authorized"
17
-
18
- result ? Success(prev_result) : Failure([failure_code, failure_message])
19
- end
20
- end
21
- end
22
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UseCases
4
- module Transaction
5
- class TransactionHandlerUndefined < StandardError; end
6
-
7
- class TransactionHandlerInvalid < StandardError; end
8
-
9
- def self.included(base)
10
- base.prepend DoCallPatch
11
- end
12
-
13
- module DoCallPatch
14
- def do_call(*args)
15
- unless respond_to?(:transaction_handler)
16
- raise TransactionHandlerUndefined, "when using *transactional*, make sure to include a transaction handler in your dependencies."
17
- end
18
-
19
- raise TransactionHandlerInvalid, "Make sure your transaction_handler implements #transaction." unless transaction_handler.respond_to?(:transaction)
20
-
21
- transaction_handler.transaction { super }
22
- end
23
- end
24
- end
25
- end
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dry/validation/contract"
4
-
5
- module UseCases
6
- module Validate
7
- class NoValidationError < StandardError; end
8
-
9
- def self.included(base)
10
- base.class_eval do
11
- extend DSL
12
- extend ClassMethods
13
- prepend CallPatch
14
- end
15
- end
16
-
17
- module CallPatch
18
- def call(*args)
19
- unless stack.include_step?(:validate)
20
- raise NoValidationError,
21
- "Make sure to define params validations by using *params*" \
22
- "*schema*, *json*, *rule* or *option* macros in your use case."
23
- end
24
-
25
- super
26
- end
27
- end
28
-
29
- module DSL
30
- def params(*args, &blk)
31
- _setup_validation
32
-
33
- _contract_class.params(*args, &blk)
34
- end
35
-
36
- def schema(*args, &blk)
37
- _setup_validation
38
-
39
- _contract_class.schema(*args, &blk)
40
- end
41
-
42
- def rule(*args, &blk)
43
- _setup_validation
44
-
45
- _contract_class.rule(*args, &blk)
46
- end
47
-
48
- def json(*args, &blk)
49
- _setup_validation
50
-
51
- _contract_class.json(*args, &blk)
52
- end
53
-
54
- def option(*args, &blk)
55
- _contract_class.option(*args, &blk)
56
- end
57
- end
58
-
59
- private
60
-
61
- def validate(params, current_user)
62
- return Failure([:validation_error, "*params* must be a hash."]) unless params.respond_to?(:merge)
63
-
64
- validation = contract.call(params)
65
-
66
- if validation.success?
67
- params.merge!(validation.to_h)
68
- Success(validation.to_h)
69
- else
70
- Failure([:validation_error, validation.errors.to_h])
71
- end
72
- end
73
-
74
- def contract
75
- return self.class._contract_class.new if self.class._contract_class_defined?
76
- end
77
-
78
- module ClassMethods
79
- def _setup_validation
80
- _define_contract_class unless _contract_class_defined?
81
- _define_validation_step unless _validation_step_defined?
82
- end
83
-
84
- def _define_validation_step
85
- step :validate
86
- end
87
-
88
- def _contract_class
89
- self::Contract
90
- end
91
-
92
- def _define_contract_class
93
- const_set(:Contract, Class.new(Dry::Validation::Contract))
94
- end
95
-
96
- def _contract_class_name
97
- "#{name}::Contract"
98
- end
99
-
100
- def _contract_class_defined?
101
- Object.const_defined? _contract_class_name
102
- end
103
-
104
- def _validation_step_defined?
105
- __steps__.map(&:name).include?(:validate)
106
- end
107
- end
108
- end
109
- end