servactory 2.0.1 → 2.0.3

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: a360afbf0c2646c3509ad3f37cb49e4a59453ded9f741a012965d962565285e4
4
- data.tar.gz: 47a2bca86fc7622a7fa5e8a70bbb9cac4c179e3a43ee67ee050e6c5dd7b0cb21
3
+ metadata.gz: d4c3af571fde0fe1cc117e71ed04c7b13ab3d212f5524ad684382e905462fc8d
4
+ data.tar.gz: 95c2ad162f85d874eb250aa1db1104e8aae685e1f738ea9b37d94f19ebc6c92a
5
5
  SHA512:
6
- metadata.gz: d6e1c489135e42759ee1bae986ed590aaf01164f8243a77b24a8a0b13d37056f4f5de3b86c1b741a94bbee8abe87656be6ad351818de32cc01a255af68d4db7e
7
- data.tar.gz: 379633b8ddee7e378eafabacc9b97ced01375daa8612ce6b162c054674a6aac825093670602a92e2b03594c76f4f5dc22de9cbec25dcfa3770b0e73f102860fa
6
+ metadata.gz: 4a1385bd8a58e5931523772619812f53168593ab5135b2c9e92f6eda4ed90097a37bd7665e56669fca6e99303d406ad9ebe68684713f63ed0b8c530cf7f5bfb9
7
+ data.tar.gz: a720e57fece727a1374802a6b419c40eca107be60325a7282aa34559b9b6e519f8891f210c0c2185ea614c70b343b881803c24a5662dc9c863100f3a2cefdb22
@@ -40,7 +40,8 @@ module Servactory
40
40
 
41
41
  private
42
42
 
43
- def getter_with(name:, &block) # rubocop:disable Metrics/MethodLength, Lint/UnusedMethodArgument, Metrics/AbcSize
43
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Lint/UnusedMethodArgument
44
+ def getter_with(name:, &block)
44
45
  input_name = name.to_s.chomp("?").to_sym
45
46
  input = @collection_of_inputs.find_by(name: input_name)
46
47
 
@@ -61,6 +62,7 @@ module Servactory
61
62
  input.value
62
63
  end
63
64
  end
65
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Lint/UnusedMethodArgument
64
66
 
65
67
  def prepare_hash_values_inside(object:, schema:) # rubocop:disable Metrics/MethodLength
66
68
  return object unless object.respond_to?(:fetch)
@@ -2,14 +2,12 @@
2
2
 
3
3
  module Servactory
4
4
  module Inputs
5
- class Input # rubocop:disable Metrics/ClassLength
5
+ class Input
6
6
  attr_accessor :value
7
7
 
8
8
  attr_reader :name,
9
9
  :internal_name,
10
- :collection_mode_class_names,
11
- :hash_mode_class_names,
12
- :option_helpers
10
+ :collection_of_options
13
11
 
14
12
  # rubocop:disable Style/KeywordParametersOrder
15
13
  def initialize(
@@ -27,14 +25,12 @@ module Servactory
27
25
  @hash_mode_class_names = hash_mode_class_names
28
26
  @option_helpers = option_helpers
29
27
 
30
- options = apply_helpers_for_options(helpers: helpers, options: options) if helpers.present?
31
-
32
- add_basic_options_with(options)
28
+ register_options(helpers: helpers, options: options)
33
29
  end
34
30
  # rubocop:enable Style/KeywordParametersOrder
35
31
 
36
32
  def method_missing(name, *args, &block)
37
- option = collection_of_options.find_by(name: name)
33
+ option = @collection_of_options.find_by(name: name)
38
34
 
39
35
  return super if option.nil?
40
36
 
@@ -42,14 +38,37 @@ module Servactory
42
38
  end
43
39
 
44
40
  def respond_to_missing?(name, *)
45
- collection_of_options.names.include?(name) || super
41
+ @collection_of_options.names.include?(name) || super
42
+ end
43
+
44
+ def register_options(helpers:, options:) # rubocop:disable Metrics/MethodLength
45
+ options = apply_helpers_for_options(helpers: helpers, options: options) if helpers.present?
46
+
47
+ options_registrar = Servactory::Maintenance::Attributes::Options::Registrar.register(
48
+ attribute: self,
49
+ collection_mode_class_names: @collection_mode_class_names,
50
+ hash_mode_class_names: @hash_mode_class_names,
51
+ options: options,
52
+ features: {
53
+ required: true,
54
+ types: true,
55
+ default: true,
56
+ collection: true,
57
+ hash: true,
58
+ inclusion: true,
59
+ must: true,
60
+ prepare: true
61
+ }
62
+ )
63
+
64
+ @collection_of_options = options_registrar.collection
46
65
  end
47
66
 
48
67
  def apply_helpers_for_options(helpers:, options:)
49
68
  prepared_options = {}
50
69
 
51
70
  helpers.each do |helper|
52
- found_helper = option_helpers.find_by(name: helper)
71
+ found_helper = @option_helpers.find_by(name: helper)
53
72
 
54
73
  next if found_helper.blank?
55
74
 
@@ -59,206 +78,12 @@ module Servactory
59
78
  options.merge(prepared_options)
60
79
  end
61
80
 
62
- def add_basic_options_with(options)
63
- # Check Class: Servactory::Inputs::Validations::Required
64
- add_required_option_with(options)
65
-
66
- # Check Class: Servactory::Inputs::Validations::Type
67
- add_types_option_with(options)
68
- add_default_option_with(options)
69
- add_collection_option_with(options)
70
- add_hash_option_with(options)
71
-
72
- # Check Class: Servactory::Inputs::Validations::Inclusion
73
- add_inclusion_option_with(options)
74
-
75
- # Check Class: Servactory::Inputs::Validations::Must
76
- add_must_option_with(options)
77
-
78
- # Check Class: nil
79
- add_prepare_option_with(options)
80
- end
81
-
82
- def add_required_option_with(options) # rubocop:disable Metrics/MethodLength
83
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
84
- name: :required,
85
- attribute: self,
86
- validation_class: Servactory::Inputs::Validations::Required,
87
- define_methods: [
88
- Servactory::Maintenance::Attributes::DefineMethod.new(
89
- name: :required?,
90
- content: ->(option:) { Servactory::Utils.true?(option[:is]) }
91
- ),
92
- Servactory::Maintenance::Attributes::DefineMethod.new(
93
- name: :optional?,
94
- content: ->(option:) { !Servactory::Utils.true?(option[:is]) }
95
- )
96
- ],
97
- define_conflicts: [
98
- Servactory::Maintenance::Attributes::DefineConflict.new(
99
- content: -> { :required_vs_default if required? && default_value_present? }
100
- )
101
- ],
102
- need_for_checks: true,
103
- body_key: :is,
104
- body_fallback: true,
105
- **options
106
- )
107
- end
108
-
109
- def add_types_option_with(options)
110
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
111
- name: :types,
112
- attribute: self,
113
- validation_class: Servactory::Inputs::Validations::Type,
114
- original_value: Array(options.fetch(:type)),
115
- need_for_checks: true,
116
- body_fallback: nil,
117
- with_advanced_mode: false
118
- )
119
- end
120
-
121
- def add_default_option_with(options) # rubocop:disable Metrics/MethodLength
122
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
123
- name: :default,
124
- attribute: self,
125
- validation_class: Servactory::Inputs::Validations::Type,
126
- define_methods: [
127
- Servactory::Maintenance::Attributes::DefineMethod.new(
128
- name: :default_value_present?,
129
- content: ->(option:) { !option.nil? }
130
- )
131
- ],
132
- need_for_checks: true,
133
- body_fallback: nil,
134
- with_advanced_mode: false,
135
- **options
136
- )
137
- end
138
-
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,
167
- validation_class: Servactory::Inputs::Validations::Type,
168
- define_methods: [
169
- Servactory::Maintenance::Attributes::DefineMethod.new(
170
- name: :hash_mode?,
171
- content: ->(**) { hash_mode_class_names.include?(options.fetch(:type)) }
172
- )
173
- ],
174
- define_conflicts: [
175
- Servactory::Maintenance::Attributes::DefineConflict.new(
176
- content: -> { :object_vs_inclusion if hash_mode? && inclusion_present? }
177
- )
178
- ],
179
- need_for_checks: false,
180
- body_key: :is,
181
- body_fallback: {},
182
- **options
183
- )
184
- end
185
-
186
- def add_inclusion_option_with(options) # rubocop:disable Metrics/MethodLength
187
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
188
- name: :inclusion,
189
- attribute: self,
190
- validation_class: Servactory::Inputs::Validations::Inclusion,
191
- define_methods: [
192
- Servactory::Maintenance::Attributes::DefineMethod.new(
193
- name: :inclusion_present?,
194
- content: ->(option:) { option[:in].is_a?(Array) && option[:in].present? }
195
- )
196
- ],
197
- need_for_checks: true,
198
- body_key: :in,
199
- body_fallback: nil,
200
- **options
201
- )
202
- end
203
-
204
- def add_must_option_with(options) # rubocop:disable Metrics/MethodLength
205
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
206
- name: :must,
207
- attribute: self,
208
- validation_class: Servactory::Inputs::Validations::Must,
209
- define_methods: [
210
- Servactory::Maintenance::Attributes::DefineMethod.new(
211
- name: :must_present?,
212
- content: ->(option:) { option.present? }
213
- )
214
- ],
215
- need_for_checks: true,
216
- body_key: :is,
217
- body_fallback: nil,
218
- with_advanced_mode: false,
219
- **options
220
- )
221
- end
222
-
223
- def add_prepare_option_with(options) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
224
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
225
- name: :prepare,
226
- attribute: self,
227
- validation_class: nil,
228
- define_methods: [
229
- Servactory::Maintenance::Attributes::DefineMethod.new(
230
- name: :prepare_present?,
231
- content: ->(option:) { option[:in].present? }
232
- )
233
- ],
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? }
243
- )
244
- ],
245
- need_for_checks: false,
246
- body_key: :in,
247
- body_fallback: false,
248
- **options
249
- )
250
- end
251
-
252
- def collection_of_options
253
- @collection_of_options ||= Servactory::Maintenance::Attributes::OptionsCollection.new
254
- end
255
-
256
81
  def options_for_checks
257
- collection_of_options.options_for_checks
82
+ @collection_of_options.options_for_checks
258
83
  end
259
84
 
260
85
  def conflict_code
261
- collection_of_options.defined_conflict_code
86
+ @collection_of_options.defined_conflict_code
262
87
  end
263
88
 
264
89
  def with_conflicts?
@@ -3,9 +3,7 @@
3
3
  module Servactory
4
4
  module Internals
5
5
  class Internal
6
- attr_reader :name,
7
- :collection_mode_class_names,
8
- :hash_mode_class_names
6
+ attr_reader :name
9
7
 
10
8
  def initialize(
11
9
  name,
@@ -17,11 +15,11 @@ module Servactory
17
15
  @collection_mode_class_names = collection_mode_class_names
18
16
  @hash_mode_class_names = hash_mode_class_names
19
17
 
20
- add_basic_options_with(options)
18
+ register_options(options: options)
21
19
  end
22
20
 
23
21
  def method_missing(name, *args, &block)
24
- option = collection_of_options.find_by(name: name)
22
+ option = @collection_of_options.find_by(name: name)
25
23
 
26
24
  return super if option.nil?
27
25
 
@@ -29,71 +27,27 @@ module Servactory
29
27
  end
30
28
 
31
29
  def respond_to_missing?(name, *)
32
- collection_of_options.names.include?(name) || super
30
+ @collection_of_options.names.include?(name) || super
33
31
  end
34
32
 
35
- def add_basic_options_with(options)
36
- # Check Class: Servactory::Internals::Validations::Type
37
- add_types_option_with(options)
38
- add_collection_option_with(options)
39
- add_hash_option_with(options)
40
- end
41
-
42
- def add_types_option_with(options)
43
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
44
- name: :types,
45
- attribute: self,
46
- validation_class: Servactory::Internals::Validations::Type,
47
- original_value: Array(options.fetch(:type)),
48
- need_for_checks: true,
49
- body_fallback: nil,
50
- with_advanced_mode: false
51
- )
52
- end
53
-
54
- def add_collection_option_with(options) # rubocop:disable Metrics/MethodLength
55
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
56
- name: :consists_of,
33
+ def register_options(options:) # rubocop:disable Metrics/MethodLength
34
+ options_registrar = Servactory::Maintenance::Attributes::Options::Registrar.register(
57
35
  attribute: self,
58
- validation_class: Servactory::Internals::Validations::Type,
59
- define_methods: [
60
- Servactory::Maintenance::Attributes::DefineMethod.new(
61
- name: :collection_mode?,
62
- content: ->(**) { collection_mode_class_names.include?(options.fetch(:type)) }
63
- )
64
- ],
65
- need_for_checks: false,
66
- body_key: :type,
67
- body_value: String,
68
- body_fallback: String,
69
- **options
36
+ collection_mode_class_names: @collection_mode_class_names,
37
+ hash_mode_class_names: @hash_mode_class_names,
38
+ options: options,
39
+ features: {
40
+ types: true,
41
+ collection: true,
42
+ hash: true
43
+ }
70
44
  )
71
- end
72
-
73
- def add_hash_option_with(options) # rubocop:disable Metrics/MethodLength
74
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
75
- name: :schema,
76
- attribute: self,
77
- validation_class: Servactory::Inputs::Validations::Type,
78
- define_methods: [
79
- Servactory::Maintenance::Attributes::DefineMethod.new(
80
- name: :hash_mode?,
81
- content: ->(**) { hash_mode_class_names.include?(options.fetch(:type)) }
82
- )
83
- ],
84
- need_for_checks: false,
85
- body_key: :is,
86
- body_fallback: {},
87
- **options
88
- )
89
- end
90
45
 
91
- def collection_of_options
92
- @collection_of_options ||= Servactory::Maintenance::Attributes::OptionsCollection.new
46
+ @collection_of_options = options_registrar.collection
93
47
  end
94
48
 
95
49
  def options_for_checks
96
- collection_of_options.options_for_checks
50
+ @collection_of_options.options_for_checks
97
51
  end
98
52
  end
99
53
  end
@@ -0,0 +1,240 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Options
7
+ class Registrar # rubocop:disable Metrics/ClassLength
8
+ DEFAULT_FEATURES = {
9
+ required: false,
10
+ types: false,
11
+ default: false,
12
+ collection: false,
13
+ hash: false,
14
+ inclusion: false,
15
+ must: false,
16
+ prepare: false
17
+ }.freeze
18
+
19
+ private_constant :DEFAULT_FEATURES
20
+
21
+ def self.register(...)
22
+ new(...).register
23
+ end
24
+
25
+ def initialize(attribute:, collection_mode_class_names:, hash_mode_class_names:, options:, features:)
26
+ @attribute = attribute
27
+ @collection_mode_class_names = collection_mode_class_names
28
+ @hash_mode_class_names = hash_mode_class_names
29
+ @options = options
30
+ @features = DEFAULT_FEATURES.merge(features)
31
+ end
32
+
33
+ ########################################################################
34
+
35
+ def register # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
36
+ # Check Class: Servactory::Inputs::Validations::Required
37
+ register_required_option if @features.fetch(:required)
38
+
39
+ # Check Class: Servactory::Inputs::Validations::Type
40
+ register_types_option if @features.fetch(:types)
41
+ register_default_option if @features.fetch(:default)
42
+ register_collection_option if @features.fetch(:collection)
43
+ register_hash_option if @features.fetch(:hash)
44
+
45
+ # Check Class: Servactory::Inputs::Validations::Inclusion
46
+ register_inclusion_option if @features.fetch(:inclusion)
47
+
48
+ # Check Class: Servactory::Inputs::Validations::Must
49
+ register_must_option if @features.fetch(:must)
50
+
51
+ # Check Class: nil
52
+ register_prepare_option if @features.fetch(:prepare)
53
+
54
+ self
55
+ end
56
+
57
+ ########################################################################
58
+
59
+ def register_required_option # rubocop:disable Metrics/MethodLength
60
+ collection << Servactory::Maintenance::Attributes::Option.new(
61
+ name: :required,
62
+ attribute: @attribute,
63
+ validation_class: Servactory::Inputs::Validations::Required,
64
+ define_methods: [
65
+ Servactory::Maintenance::Attributes::DefineMethod.new(
66
+ name: :required?,
67
+ content: ->(option:) { Servactory::Utils.true?(option[:is]) }
68
+ ),
69
+ Servactory::Maintenance::Attributes::DefineMethod.new(
70
+ name: :optional?,
71
+ content: ->(option:) { !Servactory::Utils.true?(option[:is]) }
72
+ )
73
+ ],
74
+ define_conflicts: [
75
+ Servactory::Maintenance::Attributes::DefineConflict.new(
76
+ content: -> { :required_vs_default if @attribute.required? && @attribute.default_value_present? }
77
+ )
78
+ ],
79
+ need_for_checks: true,
80
+ body_key: :is,
81
+ body_fallback: true,
82
+ **@options
83
+ )
84
+ end
85
+
86
+ def register_types_option
87
+ collection << Servactory::Maintenance::Attributes::Option.new(
88
+ name: :types,
89
+ attribute: @attribute,
90
+ validation_class: Servactory::Inputs::Validations::Type,
91
+ original_value: Array(@options.fetch(:type)),
92
+ need_for_checks: true,
93
+ body_fallback: nil,
94
+ with_advanced_mode: false
95
+ )
96
+ end
97
+
98
+ def register_default_option # rubocop:disable Metrics/MethodLength
99
+ collection << Servactory::Maintenance::Attributes::Option.new(
100
+ name: :default,
101
+ attribute: @attribute,
102
+ validation_class: Servactory::Inputs::Validations::Type,
103
+ define_methods: [
104
+ Servactory::Maintenance::Attributes::DefineMethod.new(
105
+ name: :default_value_present?,
106
+ content: ->(option:) { !option.nil? }
107
+ )
108
+ ],
109
+ need_for_checks: true,
110
+ body_fallback: nil,
111
+ with_advanced_mode: false,
112
+ **@options
113
+ )
114
+ end
115
+
116
+ def register_collection_option # rubocop:disable Metrics/MethodLength
117
+ collection << Servactory::Maintenance::Attributes::Option.new(
118
+ name: :consists_of,
119
+ attribute: @attribute,
120
+ validation_class: Servactory::Inputs::Validations::Type,
121
+ define_methods: [
122
+ Servactory::Maintenance::Attributes::DefineMethod.new(
123
+ name: :collection_mode?,
124
+ content: ->(**) { @collection_mode_class_names.include?(@options.fetch(:type)) }
125
+ )
126
+ ],
127
+ define_conflicts: [
128
+ Servactory::Maintenance::Attributes::DefineConflict.new(
129
+ content: lambda {
130
+ :collection_vs_inclusion if @attribute.collection_mode? && @attribute.inclusion_present?
131
+ }
132
+ )
133
+ ],
134
+ need_for_checks: false,
135
+ body_key: :type,
136
+ body_value: String,
137
+ body_fallback: String,
138
+ **@options
139
+ )
140
+ end
141
+
142
+ def register_hash_option # rubocop:disable Metrics/MethodLength
143
+ collection << Servactory::Maintenance::Attributes::Option.new(
144
+ name: :schema,
145
+ attribute: @attribute,
146
+ validation_class: Servactory::Inputs::Validations::Type,
147
+ define_methods: [
148
+ Servactory::Maintenance::Attributes::DefineMethod.new(
149
+ name: :hash_mode?,
150
+ content: ->(**) { @hash_mode_class_names.include?(@options.fetch(:type)) }
151
+ )
152
+ ],
153
+ define_conflicts: [
154
+ Servactory::Maintenance::Attributes::DefineConflict.new(
155
+ content: -> { :object_vs_inclusion if @attribute.hash_mode? && @attribute.inclusion_present? }
156
+ )
157
+ ],
158
+ need_for_checks: false,
159
+ body_key: :is,
160
+ body_fallback: {},
161
+ **@options
162
+ )
163
+ end
164
+
165
+ def register_inclusion_option # rubocop:disable Metrics/MethodLength
166
+ collection << Servactory::Maintenance::Attributes::Option.new(
167
+ name: :inclusion,
168
+ attribute: @attribute,
169
+ validation_class: Servactory::Inputs::Validations::Inclusion,
170
+ define_methods: [
171
+ Servactory::Maintenance::Attributes::DefineMethod.new(
172
+ name: :inclusion_present?,
173
+ content: ->(option:) { option[:in].is_a?(Array) && option[:in].present? }
174
+ )
175
+ ],
176
+ need_for_checks: true,
177
+ body_key: :in,
178
+ body_fallback: nil,
179
+ **@options
180
+ )
181
+ end
182
+
183
+ def register_must_option # rubocop:disable Metrics/MethodLength
184
+ collection << Servactory::Maintenance::Attributes::Option.new(
185
+ name: :must,
186
+ attribute: @attribute,
187
+ validation_class: Servactory::Inputs::Validations::Must,
188
+ define_methods: [
189
+ Servactory::Maintenance::Attributes::DefineMethod.new(
190
+ name: :must_present?,
191
+ content: ->(option:) { option.present? }
192
+ )
193
+ ],
194
+ need_for_checks: true,
195
+ body_key: :is,
196
+ body_fallback: nil,
197
+ with_advanced_mode: false,
198
+ **@options
199
+ )
200
+ end
201
+
202
+ def register_prepare_option # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
203
+ collection << Servactory::Maintenance::Attributes::Option.new(
204
+ name: :prepare,
205
+ attribute: @attribute,
206
+ validation_class: nil,
207
+ define_methods: [
208
+ Servactory::Maintenance::Attributes::DefineMethod.new(
209
+ name: :prepare_present?,
210
+ content: ->(option:) { option[:in].present? }
211
+ )
212
+ ],
213
+ define_conflicts: [
214
+ Servactory::Maintenance::Attributes::DefineConflict.new(
215
+ content: -> { :prepare_vs_collection if @attribute.prepare_present? && @attribute.collection_mode? }
216
+ ),
217
+ Servactory::Maintenance::Attributes::DefineConflict.new(
218
+ content: -> { :prepare_vs_inclusion if @attribute.prepare_present? && @attribute.inclusion_present? }
219
+ ),
220
+ Servactory::Maintenance::Attributes::DefineConflict.new(
221
+ content: -> { :prepare_vs_must if @attribute.prepare_present? && @attribute.must_present? }
222
+ )
223
+ ],
224
+ need_for_checks: false,
225
+ body_key: :in,
226
+ body_fallback: false,
227
+ **@options
228
+ )
229
+ end
230
+
231
+ ########################################################################
232
+
233
+ def collection
234
+ @collection ||= Servactory::Maintenance::Attributes::OptionsCollection.new
235
+ end
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end
@@ -10,10 +10,10 @@ module Servactory
10
10
  new(...).validate
11
11
  end
12
12
 
13
- def initialize(value:, types:, type:)
14
- @value = value
13
+ def initialize(attribute:, types:, value:)
14
+ @attribute = attribute
15
15
  @types = types
16
- @type = type
16
+ @value = value
17
17
 
18
18
  @errors = []
19
19
  end
@@ -39,21 +39,41 @@ module Servactory
39
39
 
40
40
  private
41
41
 
42
+ def validate_value!
43
+ return if unnecessary_types.empty?
44
+
45
+ add_error(
46
+ expected_type: attribute_consists_of_types.join(", "),
47
+ given_type: unnecessary_types.join(", ")
48
+ )
49
+ end
50
+
51
+ ########################################################################
52
+
42
53
  def prepared_type
43
54
  @prepared_type ||= @types.fetch(0, Array)
44
55
  end
45
56
 
46
- def validate_value!
47
- @value.each do |value_item|
48
- next if value_item.is_a?(@type)
57
+ def attribute_consists_of_types
58
+ @attribute_consists_of_types ||= prepared_types_from(Array(@attribute.consists_of.fetch(:type, [])))
59
+ end
49
60
 
50
- add_error(
51
- expected_type: @type,
52
- given_type: value_item.class.name
53
- )
61
+ def unnecessary_types
62
+ @unnecessary_types ||= @value&.map(&:class)&.difference(attribute_consists_of_types).presence || []
63
+ end
64
+
65
+ def prepared_types_from(types)
66
+ types.map do |type|
67
+ if type.is_a?(String)
68
+ Object.const_get(type)
69
+ else
70
+ type
71
+ end
54
72
  end
55
73
  end
56
74
 
75
+ ########################################################################
76
+
57
77
  def add_error(expected_type:, given_type:)
58
78
  @errors << {
59
79
  expected_type: expected_type,
@@ -92,23 +92,23 @@ module Servactory
92
92
  collection_validator = nil
93
93
  object_schema_validator = nil
94
94
 
95
- return if prepared_types.any? do |type|
96
- if @attribute.collection_mode?
97
- collection_validator = Servactory::Maintenance::Validations::Collection.validate(
98
- value: @value,
99
- types: @attribute.types,
100
- type: type
101
- )
95
+ if @attribute.collection_mode?
96
+ collection_validator = Servactory::Maintenance::Validations::Collection.validate(
97
+ attribute: @attribute,
98
+ types: @types,
99
+ value: @value
100
+ )
102
101
 
103
- collection_validator.valid?
104
- elsif @attribute.hash_mode?
105
- object_schema_validator = Servactory::Maintenance::Validations::ObjectSchema.validate(
106
- object: @value,
107
- schema: @attribute.schema
108
- )
102
+ return if collection_validator.valid?
103
+ elsif @attribute.hash_mode?
104
+ object_schema_validator = Servactory::Maintenance::Validations::ObjectSchema.validate(
105
+ object: @value,
106
+ schema: @attribute.schema
107
+ )
109
108
 
110
- object_schema_validator.valid?
111
- else
109
+ return if object_schema_validator.valid?
110
+ else
111
+ return if prepared_types.any? do |type| # rubocop:disable Style/IfInsideElse
112
112
  @value.is_a?(type)
113
113
  end
114
114
  end
@@ -3,9 +3,7 @@
3
3
  module Servactory
4
4
  module Outputs
5
5
  class Output
6
- attr_reader :name,
7
- :collection_mode_class_names,
8
- :hash_mode_class_names
6
+ attr_reader :name
9
7
 
10
8
  def initialize(
11
9
  name,
@@ -17,11 +15,11 @@ module Servactory
17
15
  @collection_mode_class_names = collection_mode_class_names
18
16
  @hash_mode_class_names = hash_mode_class_names
19
17
 
20
- add_basic_options_with(options)
18
+ register_options(options: options)
21
19
  end
22
20
 
23
21
  def method_missing(name, *args, &block)
24
- option = collection_of_options.find_by(name: name)
22
+ option = @collection_of_options.find_by(name: name)
25
23
 
26
24
  return super if option.nil?
27
25
 
@@ -29,71 +27,27 @@ module Servactory
29
27
  end
30
28
 
31
29
  def respond_to_missing?(name, *)
32
- collection_of_options.names.include?(name) || super
30
+ @collection_of_options.names.include?(name) || super
33
31
  end
34
32
 
35
- def add_basic_options_with(options)
36
- # Check Class: Servactory::Outputs::Validations::Type
37
- add_types_option_with(options)
38
- add_collection_option_with(options)
39
- add_hash_option_with(options)
40
- end
41
-
42
- def add_types_option_with(options)
43
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
44
- name: :types,
45
- attribute: self,
46
- validation_class: Servactory::Internals::Validations::Type,
47
- original_value: Array(options.fetch(:type)),
48
- need_for_checks: true,
49
- body_fallback: nil,
50
- with_advanced_mode: false
51
- )
52
- end
53
-
54
- def add_collection_option_with(options) # rubocop:disable Metrics/MethodLength
55
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
56
- name: :consists_of,
33
+ def register_options(options:) # rubocop:disable Metrics/MethodLength
34
+ options_registrar = Servactory::Maintenance::Attributes::Options::Registrar.register(
57
35
  attribute: self,
58
- validation_class: Servactory::Internals::Validations::Type,
59
- define_methods: [
60
- Servactory::Maintenance::Attributes::DefineMethod.new(
61
- name: :collection_mode?,
62
- content: ->(**) { collection_mode_class_names.include?(options.fetch(:type)) }
63
- )
64
- ],
65
- need_for_checks: false,
66
- body_key: :type,
67
- body_value: String,
68
- body_fallback: String,
69
- **options
36
+ collection_mode_class_names: @collection_mode_class_names,
37
+ hash_mode_class_names: @hash_mode_class_names,
38
+ options: options,
39
+ features: {
40
+ types: true,
41
+ collection: true,
42
+ hash: true
43
+ }
70
44
  )
71
- end
72
-
73
- def add_hash_option_with(options) # rubocop:disable Metrics/MethodLength
74
- collection_of_options << Servactory::Maintenance::Attributes::Option.new(
75
- name: :schema,
76
- attribute: self,
77
- validation_class: Servactory::Inputs::Validations::Type,
78
- define_methods: [
79
- Servactory::Maintenance::Attributes::DefineMethod.new(
80
- name: :hash_mode?,
81
- content: ->(**) { hash_mode_class_names.include?(options.fetch(:type)) }
82
- )
83
- ],
84
- need_for_checks: false,
85
- body_key: :is,
86
- body_fallback: {},
87
- **options
88
- )
89
- end
90
45
 
91
- def collection_of_options
92
- @collection_of_options ||= Servactory::Maintenance::Attributes::OptionsCollection.new
46
+ @collection_of_options = options_registrar.collection
93
47
  end
94
48
 
95
49
  def options_for_checks
96
- collection_of_options.options_for_checks
50
+ @collection_of_options.options_for_checks
97
51
  end
98
52
  end
99
53
  end
@@ -4,7 +4,7 @@ module Servactory
4
4
  module VERSION
5
5
  MAJOR = 2
6
6
  MINOR = 0
7
- PATCH = 1
7
+ PATCH = 3
8
8
  PRE = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-05 00:00:00.000000000 Z
11
+ date: 2023-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -277,6 +277,7 @@ files:
277
277
  - lib/servactory/maintenance/attributes/option.rb
278
278
  - lib/servactory/maintenance/attributes/option_helper.rb
279
279
  - lib/servactory/maintenance/attributes/option_helpers_collection.rb
280
+ - lib/servactory/maintenance/attributes/options/registrar.rb
280
281
  - lib/servactory/maintenance/attributes/options_collection.rb
281
282
  - lib/servactory/maintenance/collection_mode/class_names_collection.rb
282
283
  - lib/servactory/maintenance/hash_mode/class_names_collection.rb