servactory 2.0.4 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/config/locales/en.yml +3 -3
  3. data/config/locales/ru.yml +3 -3
  4. data/lib/servactory/context/workspace/internals.rb +2 -2
  5. data/lib/servactory/context/workspace/outputs.rb +2 -2
  6. data/lib/servactory/context/workspace.rb +4 -0
  7. data/lib/servactory/inputs/input.rb +20 -2
  8. data/lib/servactory/inputs/tools/validation.rb +5 -3
  9. data/lib/servactory/inputs/validations/required.rb +10 -9
  10. data/lib/servactory/inputs/workspace.rb +1 -2
  11. data/lib/servactory/internals/internal.rb +22 -1
  12. data/lib/servactory/maintenance/attributes/options/registrar.rb +11 -11
  13. data/lib/servactory/maintenance/attributes/tools/check_errors.rb +22 -0
  14. data/lib/servactory/maintenance/attributes/tools/validation.rb +81 -0
  15. data/lib/servactory/maintenance/attributes/validations/base.rb +23 -0
  16. data/lib/servactory/maintenance/attributes/validations/errors.rb +18 -0
  17. data/lib/servactory/maintenance/attributes/validations/inclusion.rb +75 -0
  18. data/lib/servactory/maintenance/attributes/validations/must.rb +102 -0
  19. data/lib/servactory/maintenance/attributes/validations/type.rb +62 -0
  20. data/lib/servactory/maintenance/validations/types.rb +10 -18
  21. data/lib/servactory/outputs/output.rb +22 -1
  22. data/lib/servactory/version.rb +2 -2
  23. metadata +8 -10
  24. data/lib/servactory/inputs/tools/check_errors.rb +0 -20
  25. data/lib/servactory/inputs/tools/distributor.rb +0 -24
  26. data/lib/servactory/inputs/validations/inclusion.rb +0 -66
  27. data/lib/servactory/inputs/validations/must.rb +0 -99
  28. data/lib/servactory/inputs/validations/type.rb +0 -52
  29. data/lib/servactory/internals/validations/base.rb +0 -17
  30. data/lib/servactory/internals/validations/type.rb +0 -31
  31. data/lib/servactory/outputs/validations/base.rb +0 -17
  32. data/lib/servactory/outputs/validations/type.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f3edad8d15393a6541eee430383fcd03da3ddf339bf6215ddf95d391487dde6
4
- data.tar.gz: 7143c15722e8d1183f523126dbf265315f182588b193f233a6f140612c532af0
3
+ metadata.gz: 1fd508e1e2a77b4d74acb00666dbc5149009fdb11f927144554f3d4d66a63887
4
+ data.tar.gz: ac3cd518c70d56f1089e2975afffedca41983555e4c54fc5d815d94ea0d1fa4c
5
5
  SHA512:
6
- metadata.gz: 0f2f96d3707a5ea5787a837e90e4c3a45ccf0464499525259e357d918ea6173b3e4318ff1f3fc70841f04bfbc2971e2f66fd378bdaf733d82e0237b5dff242ad
7
- data.tar.gz: 5aae275ca976e9a4b5d3db1bec44e2a9bcfac18ef53c7b6e6fbd15a874d89970d066073eea09369d80bacc0ce81d6a6190883178d58c92b9965def62f3be809f
6
+ metadata.gz: bcf0363a0d2d83cfd6ddffd4ea19254a51c2cf93eb33b83f79ee6129f7e38a8fe1bbedfc19cde97d3cfc78ee3d48d8550afa39c7d1d78488a10bfb8d7be609e8
7
+ data.tar.gz: 5ad30d0c89119bd1a9ccfc6b536c14276cd6197f28ef2d449f0f38a3ce3c962bba6bea7a60c4b365ff76adaa1bd284a8ddc0908b6319dc3bd895458f5d7d8ddd
@@ -4,7 +4,7 @@ en:
4
4
  undefined:
5
5
  getter: "[%{service_class_name}] Undefined input attribute `%{input_name}`"
6
6
  setter: "[%{service_class_name}] Undefined input attribute `%{input_name}`"
7
- checks:
7
+ validations:
8
8
  inclusion:
9
9
  default_error: "[%{service_class_name}] Wrong value in `%{input_name}`, must be one of `%{input_inclusion}`"
10
10
  must:
@@ -30,7 +30,7 @@ en:
30
30
  undefined:
31
31
  getter: "[%{service_class_name}] Undefined internal attribute `%{internal_name}`"
32
32
  setter: "[%{service_class_name}] Undefined internal attribute `%{internal_name}`"
33
- checks:
33
+ validations:
34
34
  type:
35
35
  default_error:
36
36
  default: "[%{service_class_name}] Wrong type of internal attribute `%{internal_name}`, expected `%{expected_type}`, got `%{given_type}`"
@@ -43,7 +43,7 @@ en:
43
43
  undefined:
44
44
  getter: "[%{service_class_name}] Undefined output attribute `%{output_name}`"
45
45
  setter: "[%{service_class_name}] Undefined output attribute `%{output_name}`"
46
- checks:
46
+ validations:
47
47
  type:
48
48
  default_error:
49
49
  default: "[%{service_class_name}] Wrong type of output attribute `%{output_name}`, expected `%{expected_type}`, got `%{given_type}`"
@@ -4,7 +4,7 @@ ru:
4
4
  undefined:
5
5
  getter: "[%{service_class_name}] Неизвестный входящий атрибут `%{input_name}`"
6
6
  setter: "[%{service_class_name}] Неизвестный входящий атрибут `%{input_name}`"
7
- checks:
7
+ validations:
8
8
  inclusion:
9
9
  default_error: "[%{service_class_name}] Неправильное значение в `%{input_name}`, должно быть одним из `%{input_inclusion}`"
10
10
  must:
@@ -31,7 +31,7 @@ ru:
31
31
  undefined:
32
32
  getter: "[%{service_class_name}] Неизвестный внутренний атрибут `%{internal_name}`"
33
33
  setter: "[%{service_class_name}] Неизвестный внутренний атрибут `%{internal_name}`"
34
- checks:
34
+ validations:
35
35
  type:
36
36
  default_error:
37
37
  default: "[%{service_class_name}] Неправильный тип внутреннего атрибута `%{internal_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
@@ -44,7 +44,7 @@ ru:
44
44
  undefined:
45
45
  getter: "[%{service_class_name}] Неизвестный выходящий атрибут `%{output_name}`"
46
46
  setter: "[%{service_class_name}] Неизвестный выходящий атрибут `%{output_name}`"
47
- checks:
47
+ validations:
48
48
  type:
49
49
  default: "[%{service_class_name}] Неправильный тип выходящего атрибута `%{output_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
50
50
  for_collection:
@@ -44,9 +44,9 @@ module Servactory
44
44
 
45
45
  return yield if internal.nil?
46
46
 
47
- Servactory::Internals::Validations::Type.validate!(
47
+ Servactory::Maintenance::Attributes::Tools::Validation.validate!(
48
48
  context: @context,
49
- internal: internal,
49
+ attribute: internal,
50
50
  value: value
51
51
  )
52
52
 
@@ -44,9 +44,9 @@ module Servactory
44
44
 
45
45
  return yield if output.nil?
46
46
 
47
- Servactory::Outputs::Validations::Type.validate!(
47
+ Servactory::Maintenance::Attributes::Tools::Validation.validate!(
48
48
  context: @context,
49
- output: output,
49
+ attribute: output,
50
50
  value: value
51
51
  )
52
52
 
@@ -36,6 +36,10 @@ module Servactory
36
36
  raise self.class.config.failure_class.new(message: message, meta: meta)
37
37
  end
38
38
 
39
+ def fail_result!(service_result)
40
+ fail!(message: service_result.error.message, meta: service_result.error.meta)
41
+ end
42
+
39
43
  private
40
44
 
41
45
  attr_reader :incoming_arguments,
@@ -3,8 +3,6 @@
3
3
  module Servactory
4
4
  module Inputs
5
5
  class Input
6
- attr_accessor :value
7
-
8
6
  attr_reader :name,
9
7
  :internal_name,
10
8
  :collection_of_options
@@ -86,9 +84,29 @@ module Servactory
86
84
  @collection_of_options.defined_conflict_code
87
85
  end
88
86
 
87
+ def system_name
88
+ self.class.name.demodulize.downcase.to_sym
89
+ end
90
+
91
+ def i18n_name
92
+ system_name.to_s.pluralize
93
+ end
94
+
89
95
  def with_conflicts?
90
96
  conflict_code.present?
91
97
  end
98
+
99
+ def input?
100
+ true
101
+ end
102
+
103
+ def internal?
104
+ false
105
+ end
106
+
107
+ def output?
108
+ false
109
+ end
92
110
  end
93
111
  end
94
112
  end
@@ -8,8 +8,9 @@ module Servactory
8
8
  new(...).validate!
9
9
  end
10
10
 
11
- def initialize(context, collection_of_inputs)
11
+ def initialize(context, incoming_arguments, collection_of_inputs)
12
12
  @context = context
13
+ @incoming_arguments = incoming_arguments
13
14
  @collection_of_inputs = collection_of_inputs
14
15
  end
15
16
 
@@ -50,7 +51,8 @@ module Servactory
50
51
  )
51
52
  validation_class.check(
52
53
  context: @context,
53
- input: input,
54
+ attribute: input,
55
+ value: @incoming_arguments.fetch(input.name, nil),
54
56
  check_key: check_key,
55
57
  check_options: check_options
56
58
  )
@@ -65,7 +67,7 @@ module Servactory
65
67
  ########################################################################
66
68
 
67
69
  def errors
68
- @errors ||= CheckErrors.new
70
+ @errors ||= Servactory::Maintenance::Attributes::Tools::CheckErrors.new
69
71
  end
70
72
 
71
73
  def raise_errors
@@ -5,7 +5,7 @@ module Servactory
5
5
  module Validations
6
6
  class Required < Base
7
7
  DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
8
- i18n_key = "servactory.inputs.checks.required.default_error."
8
+ i18n_key = "servactory.inputs.validations.required.default_error."
9
9
  i18n_key += input.collection_mode? && value.present? ? "for_collection" : "default"
10
10
 
11
11
  I18n.t(
@@ -17,10 +17,10 @@ module Servactory
17
17
 
18
18
  private_constant :DEFAULT_MESSAGE
19
19
 
20
- def self.check(context:, input:, check_key:, **)
21
- return unless should_be_checked_for?(input, check_key)
20
+ def self.check(context:, attribute:, value:, check_key:, **)
21
+ return unless should_be_checked_for?(attribute, check_key)
22
22
 
23
- new(context: context, input: input).check
23
+ new(context: context, input: attribute, value: value).check
24
24
  end
25
25
 
26
26
  def self.should_be_checked_for?(input, check_key)
@@ -29,17 +29,18 @@ module Servactory
29
29
 
30
30
  ##########################################################################
31
31
 
32
- def initialize(context:, input:)
32
+ def initialize(context:, input:, value:)
33
33
  super()
34
34
 
35
35
  @context = context
36
36
  @input = input
37
+ @value = value
37
38
  end
38
39
 
39
40
  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)
41
+ if @input.collection_mode? && Servactory::Utils.value_present?(@value)
42
+ return if @value.respond_to?(:all?) && @value.all?(&:present?)
43
+ elsif Servactory::Utils.value_present?(@value)
43
44
  return
44
45
  end
45
46
 
@@ -55,7 +56,7 @@ module Servactory
55
56
  message: message.presence || DEFAULT_MESSAGE,
56
57
  service_class_name: @context.class.name,
57
58
  input: @input,
58
- value: @input.value
59
+ value: @value
59
60
  )
60
61
  end
61
62
  end
@@ -9,9 +9,8 @@ module Servactory
9
9
  super
10
10
 
11
11
  Tools::Unnecessary.find!(self, incoming_arguments, collection_of_inputs)
12
- Tools::Distributor.assign!(incoming_arguments, collection_of_inputs)
13
12
  Tools::Rules.check!(self, collection_of_inputs)
14
- Tools::Validation.validate!(self, collection_of_inputs)
13
+ Tools::Validation.validate!(self, incoming_arguments, collection_of_inputs)
15
14
  end
16
15
  end
17
16
  end
@@ -3,7 +3,8 @@
3
3
  module Servactory
4
4
  module Internals
5
5
  class Internal
6
- attr_reader :name
6
+ attr_reader :name,
7
+ :collection_of_options
7
8
 
8
9
  def initialize(
9
10
  name,
@@ -49,6 +50,26 @@ module Servactory
49
50
  def options_for_checks
50
51
  @collection_of_options.options_for_checks
51
52
  end
53
+
54
+ def system_name
55
+ self.class.name.demodulize.downcase.to_sym
56
+ end
57
+
58
+ def i18n_name
59
+ system_name.to_s.pluralize
60
+ end
61
+
62
+ def input?
63
+ false
64
+ end
65
+
66
+ def internal?
67
+ true
68
+ end
69
+
70
+ def output?
71
+ false
72
+ end
52
73
  end
53
74
  end
54
75
  end
@@ -33,22 +33,22 @@ module Servactory
33
33
  ########################################################################
34
34
 
35
35
  def register # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
36
- # Check Class: Servactory::Inputs::Validations::Required
36
+ # Validation Class: Servactory::Inputs::Validations::Required
37
37
  register_required_option if @features.fetch(:required)
38
38
 
39
- # Check Class: Servactory::Inputs::Validations::Type
39
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Type
40
40
  register_types_option if @features.fetch(:types)
41
41
  register_default_option if @features.fetch(:default)
42
42
  register_collection_option if @features.fetch(:collection)
43
43
  register_hash_option if @features.fetch(:hash)
44
44
 
45
- # Check Class: Servactory::Inputs::Validations::Inclusion
45
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Inclusion
46
46
  register_inclusion_option if @features.fetch(:inclusion)
47
47
 
48
- # Check Class: Servactory::Inputs::Validations::Must
48
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Must
49
49
  register_must_option if @features.fetch(:must)
50
50
 
51
- # Check Class: nil
51
+ # Validation Class: nil
52
52
  register_prepare_option if @features.fetch(:prepare)
53
53
 
54
54
  self
@@ -87,7 +87,7 @@ module Servactory
87
87
  collection << Servactory::Maintenance::Attributes::Option.new(
88
88
  name: :types,
89
89
  attribute: @attribute,
90
- validation_class: Servactory::Inputs::Validations::Type,
90
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
91
91
  original_value: Array(@options.fetch(:type)),
92
92
  need_for_checks: true,
93
93
  body_fallback: nil,
@@ -99,7 +99,7 @@ module Servactory
99
99
  collection << Servactory::Maintenance::Attributes::Option.new(
100
100
  name: :default,
101
101
  attribute: @attribute,
102
- validation_class: Servactory::Inputs::Validations::Type,
102
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
103
103
  define_methods: [
104
104
  Servactory::Maintenance::Attributes::DefineMethod.new(
105
105
  name: :default_value_present?,
@@ -117,7 +117,7 @@ module Servactory
117
117
  collection << Servactory::Maintenance::Attributes::Option.new(
118
118
  name: :consists_of,
119
119
  attribute: @attribute,
120
- validation_class: Servactory::Inputs::Validations::Type,
120
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
121
121
  define_methods: [
122
122
  Servactory::Maintenance::Attributes::DefineMethod.new(
123
123
  name: :collection_mode?,
@@ -143,7 +143,7 @@ module Servactory
143
143
  collection << Servactory::Maintenance::Attributes::Option.new(
144
144
  name: :schema,
145
145
  attribute: @attribute,
146
- validation_class: Servactory::Inputs::Validations::Type,
146
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
147
147
  define_methods: [
148
148
  Servactory::Maintenance::Attributes::DefineMethod.new(
149
149
  name: :hash_mode?,
@@ -166,7 +166,7 @@ module Servactory
166
166
  collection << Servactory::Maintenance::Attributes::Option.new(
167
167
  name: :inclusion,
168
168
  attribute: @attribute,
169
- validation_class: Servactory::Inputs::Validations::Inclusion,
169
+ validation_class: Servactory::Maintenance::Attributes::Validations::Inclusion,
170
170
  define_methods: [
171
171
  Servactory::Maintenance::Attributes::DefineMethod.new(
172
172
  name: :inclusion_present?,
@@ -184,7 +184,7 @@ module Servactory
184
184
  collection << Servactory::Maintenance::Attributes::Option.new(
185
185
  name: :must,
186
186
  attribute: @attribute,
187
- validation_class: Servactory::Inputs::Validations::Must,
187
+ validation_class: Servactory::Maintenance::Attributes::Validations::Must,
188
188
  define_methods: [
189
189
  Servactory::Maintenance::Attributes::DefineMethod.new(
190
190
  name: :must_present?,
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Tools
7
+ class CheckErrors
8
+ extend Forwardable
9
+ def_delegators :@collection, :merge, :reject, :first, :empty?
10
+
11
+ def initialize(collection = Set.new)
12
+ @collection = collection
13
+ end
14
+
15
+ def not_blank
16
+ CheckErrors.new(reject(&:blank?))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Tools
7
+ class Validation
8
+ def self.validate!(...)
9
+ new(...).validate!
10
+ end
11
+
12
+ def initialize(context:, attribute:, value:)
13
+ @context = context
14
+ @attribute = attribute
15
+ @value = value
16
+ end
17
+
18
+ def validate!
19
+ process
20
+
21
+ raise_errors
22
+ end
23
+
24
+ private
25
+
26
+ def process
27
+ @attribute.options_for_checks.each do |check_key, check_options|
28
+ process_option(check_key, check_options)
29
+ end
30
+ end
31
+
32
+ def process_option(check_key, check_options)
33
+ validation_classes.each do |validation_class|
34
+ errors_from_checks = process_validation_class(
35
+ validation_class: validation_class,
36
+ check_key: check_key,
37
+ check_options: check_options
38
+ )
39
+
40
+ errors.merge(errors_from_checks.to_a)
41
+ end
42
+ end
43
+
44
+ def process_validation_class(
45
+ validation_class:,
46
+ check_key:,
47
+ check_options:
48
+ )
49
+ validation_class.check(
50
+ context: @context,
51
+ attribute: @attribute,
52
+ value: @value,
53
+ check_key: check_key,
54
+ check_options: check_options
55
+ )
56
+ end
57
+
58
+ ########################################################################
59
+
60
+ def validation_classes
61
+ @attribute.collection_of_options.validation_classes
62
+ end
63
+
64
+ ########################################################################
65
+
66
+ def errors
67
+ @errors ||= Servactory::Maintenance::Attributes::Tools::CheckErrors.new
68
+ end
69
+
70
+ def raise_errors
71
+ return if (tmp_errors = errors.not_blank).empty?
72
+
73
+ raise @context.class.config
74
+ .public_send(:"#{@attribute.system_name}_error_class")
75
+ .new(message: tmp_errors.first)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Base
8
+ private
9
+
10
+ def add_error(message:, **attributes)
11
+ message = message.call(**attributes) if message.is_a?(Proc)
12
+
13
+ errors << message
14
+ end
15
+
16
+ def errors
17
+ @errors ||= Errors.new
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Errors
8
+ extend Forwardable
9
+ def_delegators :@collection, :<<, :to_a
10
+
11
+ def initialize(*)
12
+ @collection = Set.new
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Inclusion < Base
8
+ DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
9
+ I18n.t(
10
+ "servactory.#{input.i18n_name}.validations.inclusion.default_error",
11
+ service_class_name: service_class_name,
12
+ "#{input.system_name}_name": input.name,
13
+ input_inclusion: input.inclusion[:in],
14
+ value: value
15
+ )
16
+ end
17
+
18
+ private_constant :DEFAULT_MESSAGE
19
+
20
+ def self.check(context:, attribute:, value:, check_key:, **)
21
+ return unless should_be_checked_for?(attribute, value, check_key)
22
+
23
+ new(context: context, attribute: attribute, value: value).check
24
+ end
25
+
26
+ # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
27
+ def self.should_be_checked_for?(attribute, value, check_key)
28
+ check_key == :inclusion && (
29
+ (
30
+ attribute.input? && (
31
+ attribute.required? || (
32
+ attribute.optional? && !attribute.default.nil?
33
+ ) || (
34
+ attribute.optional? && !value.nil?
35
+ )
36
+ )
37
+ ) || attribute.internal? || attribute.output?
38
+ )
39
+ end
40
+ # rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41
+
42
+ ##########################################################################
43
+
44
+ def initialize(context:, attribute:, value:)
45
+ super()
46
+
47
+ @context = context
48
+ @attribute = attribute
49
+ @value = value
50
+ end
51
+
52
+ def check
53
+ inclusion_in, message = @attribute.inclusion.values_at(:in, :message)
54
+
55
+ return if inclusion_in.nil?
56
+ return if inclusion_in.include?(@value)
57
+
58
+ add_error_with(message)
59
+ end
60
+
61
+ private
62
+
63
+ def add_error_with(message)
64
+ add_error(
65
+ message: message.presence || DEFAULT_MESSAGE,
66
+ service_class_name: @context.class.name,
67
+ "#{@attribute.system_name}": @attribute,
68
+ value: @value
69
+ )
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end