servactory 1.4.0 → 1.4.1

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: d4ffa3b926585163837e461a8a1b129a81f951f88a8d715dd6e74297e0a37ddd
4
- data.tar.gz: 1d1a3326828e282ad0f46bc76e2754fb44e0dbd5f2e00c452cd6e856866bcbbd
3
+ metadata.gz: 40fbce39ff24b8ea522a6cbd2f2b2d48a9e09034e8cc636da53827eefb254d6f
4
+ data.tar.gz: d2f56d8b47aac242932780b1969c5b47e2da961762fd7625da776ecb9966132b
5
5
  SHA512:
6
- metadata.gz: 07c4e1f255a4fabe953a3c1381229000b564b80fc0e4f1c08547fed23dcc9cb9b31ebab678d76f894ce55e5506120d412fab26bf214f433f00cf43d958b502d7
7
- data.tar.gz: 85cadb2169b968a0e9cda6f9279d9be37b01acdc0f7f91ca2b7f8458e89a219213ad317b84002d1b7684bb695a73b4c5a3a6c092a4e3a61c05984e2ee027a164
6
+ metadata.gz: 7ecefb0457e5933ddc441df45adb512da42f815ca1cdb23920835aa5f39ea4e73c7eeb83832bb3d128c83a94a21126e30619af325f7afe540195e2a44cdead7a
7
+ data.tar.gz: 5d3772b974c1d23da5d2b75443d2a1437a0d547a8eb48f538391d66368f449f8065793f152401925a753eac4f908a6c7e02cbdbc8a0b4b019415da3ff44e89b3
data/README.md CHANGED
@@ -6,23 +6,23 @@ A set of tools for building reliable services of any complexity.
6
6
 
7
7
  ## Contents
8
8
 
9
- - [Requirements](https://github.com/afuno/servactory/edit/main/README.md#requirements)
10
- - [Getting started](https://github.com/afuno/servactory/edit/main/README.md#getting-started)
11
- - [Conventions](https://github.com/afuno/servactory/edit/main/README.md#conventions)
12
- - [Installation](https://github.com/afuno/servactory/edit/main/README.md#installation)
13
- - [Preparation](https://github.com/afuno/servactory/edit/main/README.md#preparation)
14
- - [Usage](https://github.com/afuno/servactory/edit/main/README.md#usage)
15
- - [Minimal example](https://github.com/afuno/servactory#minimal-example)
16
- - [Input attributes](https://github.com/afuno/servactory#input-attributes)
17
- - [Isolated usage](https://github.com/afuno/servactory#isolated-usage)
18
- - [As an internal argument](https://github.com/afuno/servactory#isolated-usage)
19
- - [Optional inputs](https://github.com/afuno/servactory#optional-inputs)
20
- - [An array of specific values](https://github.com/afuno/servactory#an-array-of-specific-values)
21
- - [Inclusion](https://github.com/afuno/servactory#inclusion)
22
- - [Must](https://github.com/afuno/servactory#must)
23
- - [Output attributes](https://github.com/afuno/servactory/edit/main/README.md#output-attributes)
24
- - [Internal attributes](https://github.com/afuno/servactory/edit/main/README.md#internal-attributes)
25
- - [Result](https://github.com/afuno/servactory/edit/main/README.md#result)
9
+ - [Requirements](#requirements)
10
+ - [Getting started](#getting-started)
11
+ - [Conventions](#conventions)
12
+ - [Installation](#installation)
13
+ - [Preparation](#preparation)
14
+ - [Usage](#usage)
15
+ - [Minimal example](#minimal-example)
16
+ - [Input attributes](#input-attributes)
17
+ - [Isolated usage](#isolated-usage)
18
+ - [As an internal argument](#isolated-usage)
19
+ - [Optional inputs](#optional-inputs)
20
+ - [An array of specific values](#an-array-of-specific-values)
21
+ - [Inclusion](#inclusion)
22
+ - [Must](#must)
23
+ - [Output attributes](#output-attributes)
24
+ - [Internal attributes](#internal-attributes)
25
+ - [Result](#result)
26
26
 
27
27
  ## Requirements
28
28
 
@@ -14,14 +14,14 @@ module Servactory
14
14
 
15
15
  private_constant :DEFAULT_MESSAGE
16
16
 
17
- def self.check(context:, input:, value:, **)
18
- return unless should_be_checked_for?(input)
17
+ def self.check(context:, input:, value:, check_key:, **)
18
+ return unless should_be_checked_for?(input, check_key)
19
19
 
20
20
  new(context: context, input: input, value: value).check
21
21
  end
22
22
 
23
- def self.should_be_checked_for?(input)
24
- input.required?
23
+ def self.should_be_checked_for?(input, check_key)
24
+ check_key == :required && input.required?
25
25
  end
26
26
 
27
27
  ##########################################################################
@@ -34,15 +34,17 @@ module Servactory
34
34
  @value = value
35
35
  end
36
36
 
37
- def check # rubocop:disable Metrics/MethodLength
37
+ def check # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
38
38
  if @input.array? && @value.present?
39
39
  return if @value.respond_to?(:all?) && @value.all?(&:present?)
40
40
  elsif @value.present?
41
41
  return
42
42
  end
43
43
 
44
+ _, message = @input.required.values_at(:is, :message)
45
+
44
46
  add_error(
45
- DEFAULT_MESSAGE,
47
+ message.presence || DEFAULT_MESSAGE,
46
48
  service_class_name: @context.class.name,
47
49
  input: @input,
48
50
  value: @value
@@ -6,15 +6,13 @@ module Servactory
6
6
  ARRAY_DEFAULT_VALUE = ->(is: false, message: nil) { { is: is, message: message } }
7
7
 
8
8
  attr_reader :name,
9
- :collection_of_options,
10
- :types
9
+ :collection_of_options
11
10
 
12
11
  def initialize(name, collection_of_options:, type:, **options)
13
12
  @name = name
14
13
  @collection_of_options = collection_of_options
15
- @types = Array(type)
16
14
 
17
- add_basic_options_with(options)
15
+ add_basic_options_with(type: type, options: options)
18
16
 
19
17
  @collection_of_options.each do |option|
20
18
  self.class.attr_reader(:"#{option.name}")
@@ -23,13 +21,14 @@ module Servactory
23
21
  end
24
22
  end
25
23
 
26
- def add_basic_options_with(options)
24
+ def add_basic_options_with(type:, options:)
27
25
  # Check Class: Servactory::InputArguments::Checks::Required
28
26
  add_required_option_with(options)
29
27
 
30
28
  # Check Class: Servactory::InputArguments::Checks::Type
31
- add_array_option_with(options)
29
+ add_types_option_with(type)
32
30
  add_default_option_with(options)
31
+ add_array_option_with(options)
33
32
 
34
33
  # Check Class: Servactory::InputArguments::Checks::Inclusion
35
34
  add_inclusion_option_with(options)
@@ -69,28 +68,15 @@ module Servactory
69
68
  )
70
69
  end
71
70
 
72
- def add_array_option_with(options) # rubocop:disable Metrics/MethodLength
71
+ def add_types_option_with(type)
73
72
  collection_of_options << Option.new(
74
- name: :array,
73
+ name: :types,
75
74
  input: self,
75
+ original_value: Array(type),
76
76
  check_class: Servactory::InputArguments::Checks::Type,
77
- define_input_methods: lambda do
78
- <<-RUBY
79
- def array?
80
- Servactory::Utils.boolean?(array[:is])
81
- end
82
- RUBY
83
- end,
84
- define_conflicts: lambda do
85
- <<-RUBY
86
- return :array_vs_array if array? && types.include?(Array)
87
- return :array_vs_inclusion if array? && inclusion_present?
88
- RUBY
89
- end,
90
- need_for_checks: false,
91
- value_key: :is,
92
- value_fallback: false,
93
- **options
77
+ need_for_checks: true,
78
+ value_fallback: nil,
79
+ with_advanced_mode: false
94
80
  )
95
81
  end
96
82
 
@@ -113,6 +99,31 @@ module Servactory
113
99
  )
114
100
  end
115
101
 
102
+ def add_array_option_with(options) # rubocop:disable Metrics/MethodLength
103
+ collection_of_options << Option.new(
104
+ name: :array,
105
+ input: self,
106
+ check_class: Servactory::InputArguments::Checks::Type,
107
+ define_input_methods: lambda do
108
+ <<-RUBY
109
+ def array?
110
+ Servactory::Utils.boolean?(array[:is])
111
+ end
112
+ RUBY
113
+ end,
114
+ define_conflicts: lambda do
115
+ <<-RUBY
116
+ return :array_vs_array if array? && types.include?(Array)
117
+ return :array_vs_inclusion if array? && inclusion_present?
118
+ RUBY
119
+ end,
120
+ need_for_checks: false,
121
+ value_key: :is,
122
+ value_fallback: false,
123
+ **options
124
+ )
125
+ end
126
+
116
127
  def add_inclusion_option_with(options) # rubocop:disable Metrics/MethodLength
117
128
  collection_of_options << Option.new(
118
129
  name: :inclusion,
@@ -172,11 +183,7 @@ module Servactory
172
183
  end
173
184
 
174
185
  def options_for_checks
175
- {
176
- types: types
177
- }.merge(
178
- collection_of_options.options_for_checks
179
- )
186
+ collection_of_options.options_for_checks
180
187
  end
181
188
 
182
189
  def conflict_code
@@ -14,12 +14,14 @@ module Servactory
14
14
  :value_key,
15
15
  :value
16
16
 
17
+ # rubocop:disable Metrics/MethodLength
17
18
  def initialize(
18
19
  name:,
19
20
  input:,
20
21
  check_class:,
21
22
  need_for_checks:,
22
23
  value_fallback:,
24
+ original_value: nil,
23
25
  value_key: nil,
24
26
  define_input_methods: nil,
25
27
  define_conflicts: nil,
@@ -32,10 +34,16 @@ module Servactory
32
34
  @need_for_checks = need_for_checks
33
35
  @value_key = value_key
34
36
 
35
- @value = prepare_value_for(options, value_fallback: value_fallback, with_advanced_mode: with_advanced_mode)
37
+ @value = prepare_value_for(
38
+ original_value: original_value,
39
+ options: options,
40
+ value_fallback: value_fallback,
41
+ with_advanced_mode: with_advanced_mode
42
+ )
36
43
 
37
44
  input.instance_eval(define_input_methods.call) if define_input_methods.present?
38
45
  end
46
+ # rubocop:enable Metrics/MethodLength
39
47
 
40
48
  def need_for_checks?
41
49
  need_for_checks
@@ -43,7 +51,9 @@ module Servactory
43
51
 
44
52
  private
45
53
 
46
- def prepare_value_for(options, value_fallback:, with_advanced_mode:)
54
+ def prepare_value_for(original_value:, options:, value_fallback:, with_advanced_mode:)
55
+ return original_value if original_value.present?
56
+
47
57
  return options.fetch(@name, value_fallback) unless with_advanced_mode
48
58
 
49
59
  prepare_advanced_for(
@@ -4,7 +4,7 @@ module Servactory
4
4
  module VERSION
5
5
  MAJOR = 1
6
6
  MINOR = 4
7
- PATCH = 0
7
+ PATCH = 1
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov
@@ -14,30 +14,30 @@ dependencies:
14
14
  name: zeitwerk
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '2.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '2.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '7.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '7.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement