servactory 2.0.1 → 2.0.2

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: 1c804a894a51f06d11d05d6b60e539d2d1540959312330726010327dab9a40ed
4
+ data.tar.gz: ab85a273c813f528a596d79efe571d3916f694b1daff5d556d652f28dcab2c98
5
5
  SHA512:
6
- metadata.gz: d6e1c489135e42759ee1bae986ed590aaf01164f8243a77b24a8a0b13d37056f4f5de3b86c1b741a94bbee8abe87656be6ad351818de32cc01a255af68d4db7e
7
- data.tar.gz: 379633b8ddee7e378eafabacc9b97ced01375daa8612ce6b162c054674a6aac825093670602a92e2b03594c76f4f5dc22de9cbec25dcfa3770b0e73f102860fa
6
+ metadata.gz: a47f20f6976c843de99822ae9b7cd0ba17a9fc59e0d940776f8a2fe66f9145668507ba4bddb21c8a994994eab765d49f0ee656156aa4394e69c768ec831eff60
7
+ data.tar.gz: 8141c47cd8912dccc02c354cb8c8fa9026238b4c4867de25e1f32c2998aaaf2eb6e4c0ff35e765fced2d585b99f0e458993ac9dfa473674cc07007d2b2ea4033
@@ -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)
@@ -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
@@ -4,7 +4,7 @@ module Servactory
4
4
  module VERSION
5
5
  MAJOR = 2
6
6
  MINOR = 0
7
- PATCH = 1
7
+ PATCH = 2
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.2
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-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport