servactory 1.9.6 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/en.yml +19 -4
- data/config/locales/ru.yml +21 -6
- data/lib/servactory/{methods/method.rb → actions/action.rb} +2 -2
- data/lib/servactory/{methods/aliases_for_make → actions/aliases}/collection.rb +2 -2
- data/lib/servactory/{methods/method_collection.rb → actions/collection.rb} +3 -3
- data/lib/servactory/{methods → actions}/dsl.rb +9 -9
- data/lib/servactory/{methods/shortcuts_for_make → actions/shortcuts}/collection.rb +2 -2
- data/lib/servactory/actions/stages/collection.rb +20 -0
- data/lib/servactory/actions/stages/stage.rb +30 -0
- data/lib/servactory/{methods → actions}/tools/runner.rb +1 -1
- data/lib/servactory/{methods → actions}/workspace.rb +4 -2
- data/lib/servactory/base.rb +1 -1
- data/lib/servactory/configuration/dsl.rb +4 -2
- data/lib/servactory/configuration/factory.rb +12 -4
- data/lib/servactory/configuration/setup.rb +24 -8
- data/lib/servactory/context/workspace/inputs.rb +43 -7
- data/lib/servactory/context/workspace/outputs.rb +1 -1
- data/lib/servactory/context/workspace.rb +0 -4
- data/lib/servactory/dsl.rb +18 -1
- data/lib/servactory/inputs/collection.rb +1 -1
- data/lib/servactory/inputs/dsl.rb +2 -0
- data/lib/servactory/inputs/input.rb +106 -88
- data/lib/servactory/inputs/tools/distributor.rb +24 -0
- data/lib/servactory/inputs/tools/rules.rb +3 -3
- data/lib/servactory/inputs/tools/{find_unnecessary.rb → unnecessary.rb} +4 -4
- data/lib/servactory/inputs/tools/validation.rb +5 -7
- data/lib/servactory/inputs/validations/base.rb +1 -1
- data/lib/servactory/inputs/validations/inclusion.rb +15 -10
- data/lib/servactory/inputs/validations/must.rb +26 -17
- data/lib/servactory/inputs/validations/required.rb +16 -11
- data/lib/servactory/inputs/validations/type.rb +19 -69
- data/lib/servactory/inputs/workspace.rb +6 -3
- data/lib/servactory/internals/dsl.rb +6 -1
- data/lib/servactory/internals/internal.rb +81 -14
- data/lib/servactory/internals/validations/base.rb +1 -1
- data/lib/servactory/internals/validations/type.rb +6 -43
- data/lib/servactory/maintenance/attributes/define_conflict.rb +15 -0
- data/lib/servactory/maintenance/attributes/define_method.rb +17 -0
- data/lib/servactory/maintenance/attributes/option.rb +121 -0
- data/lib/servactory/maintenance/attributes/option_helper.rb +17 -0
- data/lib/servactory/maintenance/attributes/option_helpers_collection.rb +20 -0
- data/lib/servactory/maintenance/attributes/options_collection.rb +48 -0
- data/lib/servactory/maintenance/collection_mode/class_names_collection.rb +16 -0
- data/lib/servactory/maintenance/hash_mode/class_names_collection.rb +16 -0
- data/lib/servactory/maintenance/validations/collection.rb +66 -0
- data/lib/servactory/maintenance/validations/object_schema.rb +116 -0
- data/lib/servactory/maintenance/validations/types.rb +181 -0
- data/lib/servactory/outputs/dsl.rb +6 -1
- data/lib/servactory/outputs/output.rb +79 -19
- data/lib/servactory/outputs/validations/base.rb +1 -1
- data/lib/servactory/outputs/validations/type.rb +6 -45
- data/lib/servactory/version.rb +5 -4
- data/lib/servactory.rb +1 -1
- metadata +56 -30
- data/lib/servactory/inputs/define_input_conflict.rb +0 -13
- data/lib/servactory/inputs/define_input_method.rb +0 -15
- data/lib/servactory/inputs/option.rb +0 -98
- data/lib/servactory/inputs/option_helper.rb +0 -15
- data/lib/servactory/inputs/option_helpers_collection.rb +0 -18
- data/lib/servactory/inputs/options_collection.rb +0 -46
- data/lib/servactory/methods/stage.rb +0 -28
- data/lib/servactory/methods/stage_collection.rb +0 -18
@@ -3,12 +3,12 @@
|
|
3
3
|
module Servactory
|
4
4
|
module Inputs
|
5
5
|
class Input # rubocop:disable Metrics/ClassLength
|
6
|
-
|
7
|
-
|
8
|
-
private_constant :ARRAY_DEFAULT_VALUE
|
6
|
+
attr_accessor :value
|
9
7
|
|
10
8
|
attr_reader :name,
|
11
9
|
:internal_name,
|
10
|
+
:collection_mode_class_names,
|
11
|
+
:hash_mode_class_names,
|
12
12
|
:option_helpers
|
13
13
|
|
14
14
|
# rubocop:disable Style/KeywordParametersOrder
|
@@ -16,17 +16,20 @@ module Servactory
|
|
16
16
|
name,
|
17
17
|
*helpers,
|
18
18
|
as: nil,
|
19
|
-
|
19
|
+
collection_mode_class_names:,
|
20
|
+
hash_mode_class_names:,
|
20
21
|
option_helpers:,
|
21
22
|
**options
|
22
23
|
)
|
23
24
|
@name = name
|
24
25
|
@internal_name = as.present? ? as : name
|
26
|
+
@collection_mode_class_names = collection_mode_class_names
|
27
|
+
@hash_mode_class_names = hash_mode_class_names
|
25
28
|
@option_helpers = option_helpers
|
26
29
|
|
27
30
|
options = apply_helpers_for_options(helpers: helpers, options: options) if helpers.present?
|
28
31
|
|
29
|
-
add_basic_options_with(
|
32
|
+
add_basic_options_with(options)
|
30
33
|
end
|
31
34
|
# rubocop:enable Style/KeywordParametersOrder
|
32
35
|
|
@@ -35,7 +38,7 @@ module Servactory
|
|
35
38
|
|
36
39
|
return super if option.nil?
|
37
40
|
|
38
|
-
option.
|
41
|
+
option.body
|
39
42
|
end
|
40
43
|
|
41
44
|
def respond_to_missing?(name, *)
|
@@ -56,14 +59,15 @@ module Servactory
|
|
56
59
|
options.merge(prepared_options)
|
57
60
|
end
|
58
61
|
|
59
|
-
def add_basic_options_with(
|
62
|
+
def add_basic_options_with(options)
|
60
63
|
# Check Class: Servactory::Inputs::Validations::Required
|
61
64
|
add_required_option_with(options)
|
62
65
|
|
63
66
|
# Check Class: Servactory::Inputs::Validations::Type
|
64
|
-
add_types_option_with(
|
67
|
+
add_types_option_with(options)
|
65
68
|
add_default_option_with(options)
|
66
|
-
|
69
|
+
add_collection_option_with(options)
|
70
|
+
add_hash_option_with(options)
|
67
71
|
|
68
72
|
# Check Class: Servactory::Inputs::Validations::Inclusion
|
69
73
|
add_inclusion_option_with(options)
|
@@ -73,166 +77,180 @@ module Servactory
|
|
73
77
|
|
74
78
|
# Check Class: nil
|
75
79
|
add_prepare_option_with(options)
|
76
|
-
add_internal_option_with(options)
|
77
80
|
end
|
78
81
|
|
79
82
|
def add_required_option_with(options) # rubocop:disable Metrics/MethodLength
|
80
|
-
collection_of_options << Option.new(
|
83
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
81
84
|
name: :required,
|
82
|
-
|
85
|
+
attribute: self,
|
83
86
|
validation_class: Servactory::Inputs::Validations::Required,
|
84
|
-
|
85
|
-
|
87
|
+
define_methods: [
|
88
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
86
89
|
name: :required?,
|
87
|
-
content: ->(
|
90
|
+
content: ->(option:) { Servactory::Utils.true?(option[:is]) }
|
88
91
|
),
|
89
|
-
|
92
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
90
93
|
name: :optional?,
|
91
|
-
content: ->(
|
94
|
+
content: ->(option:) { !Servactory::Utils.true?(option[:is]) }
|
92
95
|
)
|
93
96
|
],
|
94
|
-
|
95
|
-
|
97
|
+
define_conflicts: [
|
98
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
99
|
+
content: -> { :required_vs_default if required? && default_value_present? }
|
100
|
+
)
|
96
101
|
],
|
97
102
|
need_for_checks: true,
|
98
|
-
|
99
|
-
|
103
|
+
body_key: :is,
|
104
|
+
body_fallback: true,
|
100
105
|
**options
|
101
106
|
)
|
102
107
|
end
|
103
108
|
|
104
|
-
def add_types_option_with(
|
105
|
-
collection_of_options << Option.new(
|
109
|
+
def add_types_option_with(options)
|
110
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
106
111
|
name: :types,
|
107
|
-
|
112
|
+
attribute: self,
|
108
113
|
validation_class: Servactory::Inputs::Validations::Type,
|
109
|
-
original_value: Array(type),
|
114
|
+
original_value: Array(options.fetch(:type)),
|
110
115
|
need_for_checks: true,
|
111
|
-
|
116
|
+
body_fallback: nil,
|
112
117
|
with_advanced_mode: false
|
113
118
|
)
|
114
119
|
end
|
115
120
|
|
116
121
|
def add_default_option_with(options) # rubocop:disable Metrics/MethodLength
|
117
|
-
collection_of_options << Option.new(
|
122
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
118
123
|
name: :default,
|
119
|
-
|
124
|
+
attribute: self,
|
120
125
|
validation_class: Servactory::Inputs::Validations::Type,
|
121
|
-
|
122
|
-
|
126
|
+
define_methods: [
|
127
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
123
128
|
name: :default_value_present?,
|
124
|
-
content: ->(
|
129
|
+
content: ->(option:) { !option.nil? }
|
125
130
|
)
|
126
131
|
],
|
127
132
|
need_for_checks: true,
|
128
|
-
|
133
|
+
body_fallback: nil,
|
129
134
|
with_advanced_mode: false,
|
130
135
|
**options
|
131
136
|
)
|
132
137
|
end
|
133
138
|
|
134
|
-
def
|
135
|
-
collection_of_options << Option.new(
|
136
|
-
name: :
|
137
|
-
|
139
|
+
def add_collection_option_with(options) # rubocop:disable Metrics/MethodLength
|
140
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
141
|
+
name: :consists_of,
|
142
|
+
attribute: self,
|
143
|
+
validation_class: Servactory::Inputs::Validations::Type,
|
144
|
+
define_methods: [
|
145
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
146
|
+
name: :collection_mode?,
|
147
|
+
content: ->(**) { collection_mode_class_names.include?(options.fetch(:type)) }
|
148
|
+
)
|
149
|
+
],
|
150
|
+
define_conflicts: [
|
151
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
152
|
+
content: -> { :collection_vs_inclusion if collection_mode? && inclusion_present? }
|
153
|
+
)
|
154
|
+
],
|
155
|
+
need_for_checks: false,
|
156
|
+
body_key: :type,
|
157
|
+
body_value: String,
|
158
|
+
body_fallback: String,
|
159
|
+
**options
|
160
|
+
)
|
161
|
+
end
|
162
|
+
|
163
|
+
def add_hash_option_with(options) # rubocop:disable Metrics/MethodLength
|
164
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
165
|
+
name: :schema,
|
166
|
+
attribute: self,
|
138
167
|
validation_class: Servactory::Inputs::Validations::Type,
|
139
|
-
|
140
|
-
|
141
|
-
name: :
|
142
|
-
content: ->(
|
168
|
+
define_methods: [
|
169
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
170
|
+
name: :hash_mode?,
|
171
|
+
content: ->(**) { hash_mode_class_names.include?(options.fetch(:type)) }
|
143
172
|
)
|
144
173
|
],
|
145
|
-
|
146
|
-
|
147
|
-
|
174
|
+
define_conflicts: [
|
175
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
176
|
+
content: -> { :object_vs_inclusion if hash_mode? && inclusion_present? }
|
177
|
+
)
|
148
178
|
],
|
149
179
|
need_for_checks: false,
|
150
|
-
|
151
|
-
|
180
|
+
body_key: :is,
|
181
|
+
body_fallback: {},
|
152
182
|
**options
|
153
183
|
)
|
154
184
|
end
|
155
185
|
|
156
186
|
def add_inclusion_option_with(options) # rubocop:disable Metrics/MethodLength
|
157
|
-
collection_of_options << Option.new(
|
187
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
158
188
|
name: :inclusion,
|
159
|
-
|
189
|
+
attribute: self,
|
160
190
|
validation_class: Servactory::Inputs::Validations::Inclusion,
|
161
|
-
|
162
|
-
|
191
|
+
define_methods: [
|
192
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
163
193
|
name: :inclusion_present?,
|
164
|
-
content: ->(
|
194
|
+
content: ->(option:) { option[:in].is_a?(Array) && option[:in].present? }
|
165
195
|
)
|
166
196
|
],
|
167
197
|
need_for_checks: true,
|
168
|
-
|
169
|
-
|
198
|
+
body_key: :in,
|
199
|
+
body_fallback: nil,
|
170
200
|
**options
|
171
201
|
)
|
172
202
|
end
|
173
203
|
|
174
204
|
def add_must_option_with(options) # rubocop:disable Metrics/MethodLength
|
175
|
-
collection_of_options << Option.new(
|
205
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
176
206
|
name: :must,
|
177
|
-
|
207
|
+
attribute: self,
|
178
208
|
validation_class: Servactory::Inputs::Validations::Must,
|
179
|
-
|
180
|
-
|
209
|
+
define_methods: [
|
210
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
181
211
|
name: :must_present?,
|
182
|
-
content: ->(
|
212
|
+
content: ->(option:) { option.present? }
|
183
213
|
)
|
184
214
|
],
|
185
215
|
need_for_checks: true,
|
186
|
-
|
187
|
-
|
216
|
+
body_key: :is,
|
217
|
+
body_fallback: nil,
|
188
218
|
with_advanced_mode: false,
|
189
219
|
**options
|
190
220
|
)
|
191
221
|
end
|
192
222
|
|
193
223
|
def add_prepare_option_with(options) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
194
|
-
collection_of_options << Option.new(
|
224
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
195
225
|
name: :prepare,
|
196
|
-
|
226
|
+
attribute: self,
|
197
227
|
validation_class: nil,
|
198
|
-
|
199
|
-
|
228
|
+
define_methods: [
|
229
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
200
230
|
name: :prepare_present?,
|
201
|
-
content: ->(
|
231
|
+
content: ->(option:) { option[:in].present? }
|
202
232
|
)
|
203
233
|
],
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
)
|
214
|
-
end
|
215
|
-
|
216
|
-
def add_internal_option_with(options) # rubocop:disable Metrics/MethodLength
|
217
|
-
collection_of_options << Option.new(
|
218
|
-
name: :internal,
|
219
|
-
input: self,
|
220
|
-
validation_class: nil,
|
221
|
-
define_input_methods: [
|
222
|
-
DefineInputMethod.new(
|
223
|
-
name: :internal?,
|
224
|
-
content: ->(value:) { Servactory::Utils.true?(value[:is]) }
|
234
|
+
define_conflicts: [
|
235
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
236
|
+
content: -> { :prepare_vs_collection if prepare_present? && collection_mode? }
|
237
|
+
),
|
238
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
239
|
+
content: -> { :prepare_vs_inclusion if prepare_present? && inclusion_present? }
|
240
|
+
),
|
241
|
+
Servactory::Maintenance::Attributes::DefineConflict.new(
|
242
|
+
content: -> { :prepare_vs_must if prepare_present? && must_present? }
|
225
243
|
)
|
226
244
|
],
|
227
245
|
need_for_checks: false,
|
228
|
-
|
229
|
-
|
246
|
+
body_key: :in,
|
247
|
+
body_fallback: false,
|
230
248
|
**options
|
231
249
|
)
|
232
250
|
end
|
233
251
|
|
234
252
|
def collection_of_options
|
235
|
-
@collection_of_options ||= OptionsCollection.new
|
253
|
+
@collection_of_options ||= Servactory::Maintenance::Attributes::OptionsCollection.new
|
236
254
|
end
|
237
255
|
|
238
256
|
def options_for_checks
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Inputs
|
5
|
+
module Tools
|
6
|
+
class Distributor
|
7
|
+
def self.assign!(...)
|
8
|
+
new(...).assign!
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(incoming_arguments, collection_of_inputs)
|
12
|
+
@incoming_arguments = incoming_arguments
|
13
|
+
@collection_of_inputs = collection_of_inputs
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign!
|
17
|
+
@collection_of_inputs.map do |input|
|
18
|
+
input.value = @incoming_arguments.fetch(input.name, nil)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -4,8 +4,8 @@ module Servactory
|
|
4
4
|
module Inputs
|
5
5
|
module Tools
|
6
6
|
class Rules
|
7
|
-
def self.
|
8
|
-
new(...).
|
7
|
+
def self.check!(...)
|
8
|
+
new(...).check!
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(context, collection_of_inputs)
|
@@ -13,7 +13,7 @@ module Servactory
|
|
13
13
|
@collection_of_inputs = collection_of_inputs
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def check!
|
17
17
|
@collection_of_inputs.each do |input|
|
18
18
|
check_for!(input)
|
19
19
|
end
|
@@ -3,9 +3,9 @@
|
|
3
3
|
module Servactory
|
4
4
|
module Inputs
|
5
5
|
module Tools
|
6
|
-
class
|
7
|
-
def self.
|
8
|
-
new(...).
|
6
|
+
class Unnecessary
|
7
|
+
def self.find!(...)
|
8
|
+
new(...).find!
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(context, incoming_arguments, collection_of_inputs)
|
@@ -14,7 +14,7 @@ module Servactory
|
|
14
14
|
@collection_of_inputs = collection_of_inputs
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def find!
|
18
18
|
return if unnecessary_attributes.empty?
|
19
19
|
|
20
20
|
message_text = I18n.t(
|
@@ -8,9 +8,8 @@ module Servactory
|
|
8
8
|
new(...).validate!
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(context,
|
11
|
+
def initialize(context, collection_of_inputs)
|
12
12
|
@context = context
|
13
|
-
@incoming_arguments = incoming_arguments
|
14
13
|
@collection_of_inputs = collection_of_inputs
|
15
14
|
end
|
16
15
|
|
@@ -32,8 +31,8 @@ module Servactory
|
|
32
31
|
|
33
32
|
def process_option(check_key, check_options, input:)
|
34
33
|
validation_classes_from(input).each do |validation_class|
|
35
|
-
errors_from_checks =
|
36
|
-
validation_class,
|
34
|
+
errors_from_checks = process_validation_class(
|
35
|
+
validation_class: validation_class,
|
37
36
|
input: input,
|
38
37
|
check_key: check_key,
|
39
38
|
check_options: check_options
|
@@ -43,8 +42,8 @@ module Servactory
|
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
|
-
def
|
47
|
-
validation_class
|
45
|
+
def process_validation_class(
|
46
|
+
validation_class:,
|
48
47
|
input:,
|
49
48
|
check_key:,
|
50
49
|
check_options:
|
@@ -52,7 +51,6 @@ module Servactory
|
|
52
51
|
validation_class.check(
|
53
52
|
context: @context,
|
54
53
|
input: input,
|
55
|
-
value: @incoming_arguments.fetch(input.name, nil),
|
56
54
|
check_key: check_key,
|
57
55
|
check_options: check_options
|
58
56
|
)
|
@@ -16,43 +16,48 @@ module Servactory
|
|
16
16
|
|
17
17
|
private_constant :DEFAULT_MESSAGE
|
18
18
|
|
19
|
-
def self.check(context:, input:,
|
20
|
-
return unless should_be_checked_for?(input,
|
19
|
+
def self.check(context:, input:, check_key:, **)
|
20
|
+
return unless should_be_checked_for?(input, check_key)
|
21
21
|
|
22
|
-
new(context: context, input: input
|
22
|
+
new(context: context, input: input).check
|
23
23
|
end
|
24
24
|
|
25
|
-
def self.should_be_checked_for?(input,
|
25
|
+
def self.should_be_checked_for?(input, check_key)
|
26
26
|
check_key == :inclusion && (
|
27
27
|
input.required? || (
|
28
28
|
input.optional? && !input.default.nil?
|
29
29
|
) || (
|
30
|
-
input.optional? && !value.nil?
|
30
|
+
input.optional? && !input.value.nil?
|
31
31
|
)
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
35
35
|
##########################################################################
|
36
36
|
|
37
|
-
def initialize(context:, input
|
37
|
+
def initialize(context:, input:)
|
38
38
|
super()
|
39
39
|
|
40
40
|
@context = context
|
41
41
|
@input = input
|
42
|
-
@value = value
|
43
42
|
end
|
44
43
|
|
45
44
|
def check
|
46
45
|
inclusion_in, message = @input.inclusion.values_at(:in, :message)
|
47
46
|
|
48
47
|
return if inclusion_in.nil?
|
49
|
-
return if inclusion_in.include?(@value)
|
48
|
+
return if inclusion_in.include?(@input.value)
|
50
49
|
|
50
|
+
add_error_with(message)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def add_error_with(message)
|
51
56
|
add_error(
|
52
|
-
message.presence || DEFAULT_MESSAGE,
|
57
|
+
message: message.presence || DEFAULT_MESSAGE,
|
53
58
|
service_class_name: @context.class.name,
|
54
59
|
input: @input,
|
55
|
-
value: @value
|
60
|
+
value: @input.value
|
56
61
|
)
|
57
62
|
end
|
58
63
|
end
|
@@ -27,10 +27,10 @@ module Servactory
|
|
27
27
|
|
28
28
|
private_constant :DEFAULT_MESSAGE, :SYNTAX_ERROR_MESSAGE
|
29
29
|
|
30
|
-
def self.check(context:, input:,
|
30
|
+
def self.check(context:, input:, check_key:, check_options:)
|
31
31
|
return unless should_be_checked_for?(input, check_key)
|
32
32
|
|
33
|
-
new(context: context, input: input,
|
33
|
+
new(context: context, input: input, check_options: check_options).check
|
34
34
|
end
|
35
35
|
|
36
36
|
def self.should_be_checked_for?(input, check_key)
|
@@ -39,28 +39,21 @@ module Servactory
|
|
39
39
|
|
40
40
|
##########################################################################
|
41
41
|
|
42
|
-
def initialize(context:, input:,
|
42
|
+
def initialize(context:, input:, check_options:)
|
43
43
|
super()
|
44
44
|
|
45
45
|
@context = context
|
46
46
|
@input = input
|
47
|
-
@value = value
|
48
47
|
@check_options = check_options
|
49
48
|
end
|
50
49
|
|
51
|
-
def check
|
50
|
+
def check
|
52
51
|
@check_options.each do |code, options|
|
53
52
|
message = call_or_fetch_message_from(code, options)
|
54
53
|
|
55
54
|
next if message.blank?
|
56
55
|
|
57
|
-
|
58
|
-
message,
|
59
|
-
service_class_name: @context.class.name,
|
60
|
-
input: @input,
|
61
|
-
value: @value,
|
62
|
-
code: code
|
63
|
-
)
|
56
|
+
add_error_with(message, code)
|
64
57
|
end
|
65
58
|
|
66
59
|
errors
|
@@ -68,20 +61,36 @@ module Servactory
|
|
68
61
|
|
69
62
|
private
|
70
63
|
|
71
|
-
def call_or_fetch_message_from(code, options)
|
64
|
+
def call_or_fetch_message_from(code, options)
|
72
65
|
check, message = options.values_at(:is, :message)
|
73
66
|
|
74
|
-
return if check.call(value: @value)
|
67
|
+
return if check.call(value: @input.value)
|
75
68
|
|
76
69
|
message.presence || DEFAULT_MESSAGE
|
77
70
|
rescue StandardError => e
|
71
|
+
add_syntax_error_with(SYNTAX_ERROR_MESSAGE, code, e.message)
|
72
|
+
end
|
73
|
+
|
74
|
+
########################################################################
|
75
|
+
|
76
|
+
def add_error_with(message, code)
|
78
77
|
add_error(
|
79
|
-
|
78
|
+
message: message,
|
80
79
|
service_class_name: @context.class.name,
|
81
80
|
input: @input,
|
82
|
-
value: @value,
|
81
|
+
value: @input.value,
|
82
|
+
code: code
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_syntax_error_with(message, code, exception_message)
|
87
|
+
add_error(
|
88
|
+
message: message,
|
89
|
+
service_class_name: @context.class.name,
|
90
|
+
input: @input,
|
91
|
+
value: @input.value,
|
83
92
|
code: code,
|
84
|
-
exception_message:
|
93
|
+
exception_message: exception_message
|
85
94
|
)
|
86
95
|
end
|
87
96
|
end
|
@@ -6,7 +6,7 @@ module Servactory
|
|
6
6
|
class Required < Base
|
7
7
|
DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
|
8
8
|
i18n_key = "servactory.inputs.checks.required.default_error."
|
9
|
-
i18n_key += input.
|
9
|
+
i18n_key += input.collection_mode? && value.present? ? "for_collection" : "default"
|
10
10
|
|
11
11
|
I18n.t(
|
12
12
|
i18n_key,
|
@@ -17,10 +17,10 @@ module Servactory
|
|
17
17
|
|
18
18
|
private_constant :DEFAULT_MESSAGE
|
19
19
|
|
20
|
-
def self.check(context:, input:,
|
20
|
+
def self.check(context:, input:, check_key:, **)
|
21
21
|
return unless should_be_checked_for?(input, check_key)
|
22
22
|
|
23
|
-
new(context: context, input: input
|
23
|
+
new(context: context, input: input).check
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.should_be_checked_for?(input, check_key)
|
@@ -29,28 +29,33 @@ module Servactory
|
|
29
29
|
|
30
30
|
##########################################################################
|
31
31
|
|
32
|
-
def initialize(context:, input
|
32
|
+
def initialize(context:, input:)
|
33
33
|
super()
|
34
34
|
|
35
35
|
@context = context
|
36
36
|
@input = input
|
37
|
-
@value = value
|
38
37
|
end
|
39
38
|
|
40
|
-
def check
|
41
|
-
if @input.
|
42
|
-
return if @value.respond_to?(:all?) && @value.all?(&:present?)
|
43
|
-
elsif Servactory::Utils.value_present?(@value)
|
39
|
+
def check
|
40
|
+
if @input.collection_mode? && Servactory::Utils.value_present?(@input.value)
|
41
|
+
return if @input.value.respond_to?(:all?) && @input.value.all?(&:present?)
|
42
|
+
elsif Servactory::Utils.value_present?(@input.value)
|
44
43
|
return
|
45
44
|
end
|
46
45
|
|
47
46
|
_, message = @input.required.values_at(:is, :message)
|
48
47
|
|
48
|
+
add_error_with(message)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def add_error_with(message)
|
49
54
|
add_error(
|
50
|
-
message.presence || DEFAULT_MESSAGE,
|
55
|
+
message: message.presence || DEFAULT_MESSAGE,
|
51
56
|
service_class_name: @context.class.name,
|
52
57
|
input: @input,
|
53
|
-
value: @value
|
58
|
+
value: @input.value
|
54
59
|
)
|
55
60
|
end
|
56
61
|
end
|