pathway 0.12.3 → 1.1.0

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,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pathway
4
- module Plugins
5
- module DryValidation
6
- module V1_0
7
- module ClassMethods
8
- attr_reader :contract_class, :contract_options
9
- attr_accessor :auto_wire
10
-
11
- alias_method :auto_wire_options, :auto_wire
12
- alias_method :auto_wire_options=, :auto_wire=
13
-
14
- def contract(base = nil, &block)
15
- if block_given?
16
- base ||= _base_contract
17
- self.contract_class = Class.new(base, &block)
18
- elsif base
19
- self.contract_class = base
20
- else
21
- raise ArgumentError, 'Either a contract class or a block must be provided'
22
- end
23
- end
24
-
25
- ruby2_keywords def params(*args, &block)
26
- contract { params(*args, &block) }
27
- end
28
-
29
- def contract_class= klass
30
- @contract_class = klass
31
- @contract_options = (klass.dry_initializer.options - Dry::Validation::Contract.dry_initializer.options).map(&:target)
32
- @builded_contract = @contract_options.empty? && klass.schema ? klass.new : nil
33
- end
34
-
35
- def build_contract(**opts)
36
- @builded_contract || contract_class.new(**opts)
37
- end
38
-
39
- def inherited(subclass)
40
- super
41
- subclass.auto_wire = auto_wire
42
- subclass.contract_class = contract_class
43
- end
44
-
45
- private
46
-
47
- def _base_contract
48
- superclass.respond_to?(:contract_class) ? superclass.contract_class : Dry::Validation::Contract
49
- end
50
- end
51
-
52
- module InstanceMethods
53
- extend Forwardable
54
-
55
- delegate %i[build_contract contract_options auto_wire_options auto_wire] => 'self.class'
56
- alias_method :contract, :build_contract
57
-
58
- def validate(state, with: nil)
59
- if auto_wire && contract_options.any?
60
- with ||= contract_options.zip(contract_options).to_h
61
- end
62
- opts = Hash(with).map { |to, from| [to, state[from]] }.to_h
63
- validate_with(state[:input], **opts)
64
- .then { |params| state.update(params: params) }
65
- end
66
-
67
- def validate_with(input, **opts)
68
- result = contract(**opts).call(input)
69
-
70
- result.success? ? wrap(result.values.to_h) : error(:validation, details: result.errors.to_h)
71
- end
72
- end
73
-
74
- def self.apply(operation, auto_wire_options: (auto_wire_options_was_not_used=true; false), auto_wire: auto_wire_options)
75
- #:nocov:
76
- unless auto_wire_options_was_not_used
77
- warn "[DEPRECATION] `auto_wire_options` is deprecated. Please use `auto_wire` instead"
78
- end
79
- #:nocov:
80
-
81
- operation.auto_wire = auto_wire
82
- operation.contract_class = Dry::Validation::Contract
83
- end
84
- end
85
- end
86
- end
87
- end