servactory 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -19
- data/lib/servactory/context/dsl.rb +1 -1
- data/lib/servactory/input_arguments/checks/required.rb +8 -6
- data/lib/servactory/input_arguments/input_argument.rb +37 -30
- data/lib/servactory/input_arguments/option.rb +12 -2
- data/lib/servactory/input_arguments/tools/prepare.rb +10 -11
- data/lib/servactory/inputs.rb +4 -2
- data/lib/servactory/version.rb +1 -1
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 781548f2f3cda01476bfa6570d3b1fac94890c3b86271f6bc58a32542f2d2ce9
|
4
|
+
data.tar.gz: fdddb44644b45ce539193db90e86bfefc63b3fd8140b2a54bd8654d22027191a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60bc160221bfad63233635071ddb345f814c9ca1bdab3867cb5d3ae58835b43bca0c6a7928200a2553995bf836cfe1592d42f3dbfaa48ea97e44af8ae5eb75e2
|
7
|
+
data.tar.gz: 8a272ec82df4539946462099c9dd45857d1583b1526f74fa78d05e1a8896762e61a580bac4016ea0b78c62bb1ebf16480e9992dfadd4c99950b0938557a68f54
|
data/README.md
CHANGED
@@ -3,26 +3,27 @@
|
|
3
3
|
A set of tools for building reliable services of any complexity.
|
4
4
|
|
5
5
|
[](https://rubygems.org/gems/servactory)
|
6
|
+
[](https://github.com/afuno/servactory/releases)
|
6
7
|
|
7
8
|
## Contents
|
8
9
|
|
9
|
-
- [Requirements](
|
10
|
-
- [Getting started](
|
11
|
-
- [Conventions](
|
12
|
-
- [Installation](
|
13
|
-
- [Preparation](
|
14
|
-
- [Usage](
|
15
|
-
- [Minimal example](
|
16
|
-
- [Input attributes](
|
17
|
-
- [Isolated usage](
|
18
|
-
- [As an internal argument](
|
19
|
-
- [Optional inputs](
|
20
|
-
- [An array of specific values](
|
21
|
-
- [Inclusion](
|
22
|
-
- [Must](
|
23
|
-
- [Output attributes](
|
24
|
-
- [Internal attributes](
|
25
|
-
- [Result](
|
10
|
+
- [Requirements](#requirements)
|
11
|
+
- [Getting started](#getting-started)
|
12
|
+
- [Conventions](#conventions)
|
13
|
+
- [Installation](#installation)
|
14
|
+
- [Preparation](#preparation)
|
15
|
+
- [Usage](#usage)
|
16
|
+
- [Minimal example](#minimal-example)
|
17
|
+
- [Input attributes](#input-attributes)
|
18
|
+
- [Isolated usage](#isolated-usage)
|
19
|
+
- [As an internal argument](#isolated-usage)
|
20
|
+
- [Optional inputs](#optional-inputs)
|
21
|
+
- [An array of specific values](#an-array-of-specific-values)
|
22
|
+
- [Inclusion](#inclusion)
|
23
|
+
- [Must](#must)
|
24
|
+
- [Output attributes](#output-attributes)
|
25
|
+
- [Internal attributes](#internal-attributes)
|
26
|
+
- [Result](#result)
|
26
27
|
|
27
28
|
## Requirements
|
28
29
|
|
@@ -93,16 +94,18 @@ end
|
|
93
94
|
|
94
95
|
```ruby
|
95
96
|
class MinimalService < ApplicationService::Base
|
96
|
-
stage { make :
|
97
|
+
stage { make :call }
|
97
98
|
|
98
99
|
private
|
99
100
|
|
100
|
-
def
|
101
|
+
def call
|
101
102
|
# ...
|
102
103
|
end
|
103
104
|
end
|
104
105
|
```
|
105
106
|
|
107
|
+
[More examples](https://github.com/afuno/servactory/tree/main/examples/usual)
|
108
|
+
|
106
109
|
### Input attributes
|
107
110
|
|
108
111
|
#### Isolated usage
|
@@ -8,7 +8,7 @@ module Servactory
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
-
def call!(arguments) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
11
|
+
def call!(arguments = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
12
12
|
@context_store ||= Store.new(self)
|
13
13
|
|
14
14
|
assign_data_with(arguments)
|
@@ -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
|
-
|
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
|
71
|
+
def add_types_option_with(type)
|
73
72
|
collection_of_options << Option.new(
|
74
|
-
name: :
|
73
|
+
name: :types,
|
75
74
|
input: self,
|
75
|
+
original_value: Array(type),
|
76
76
|
check_class: Servactory::InputArguments::Checks::Type,
|
77
|
-
|
78
|
-
|
79
|
-
|
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(
|
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
|
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(
|
@@ -39,9 +39,10 @@ module Servactory
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def create_instance_variables
|
42
|
-
Servactory::Inputs.
|
42
|
+
inputs_class = Servactory::Inputs.dup
|
43
|
+
inputs_class.class_eval(class_inputs_template) if class_inputs_template.present?
|
43
44
|
|
44
|
-
@context.assign_inputs(
|
45
|
+
@context.assign_inputs(inputs_class.new(**@inputs_variables))
|
45
46
|
|
46
47
|
@context.class.class_eval(context_internal_variables_template) if context_internal_variables_template.present?
|
47
48
|
|
@@ -54,10 +55,6 @@ module Servactory
|
|
54
55
|
|
55
56
|
# EXAMPLE:
|
56
57
|
#
|
57
|
-
# attr_reader(*[:attr_1]); def initialize(attr_1); @attr_1 = attr_1; end
|
58
|
-
#
|
59
|
-
# OR
|
60
|
-
#
|
61
58
|
# attr_reader(*[:attr_1, :attr_2, :attr_3])
|
62
59
|
#
|
63
60
|
# def initialize(attr_1:, attr_2:, attr_3:)
|
@@ -65,18 +62,20 @@ module Servactory
|
|
65
62
|
# end
|
66
63
|
#
|
67
64
|
def class_inputs_template
|
68
|
-
|
69
|
-
|
65
|
+
return if @inputs_variables.blank?
|
66
|
+
|
67
|
+
@class_inputs_template ||= <<-RUBY
|
68
|
+
attr_reader(*#{@inputs_variables.keys})
|
70
69
|
|
71
|
-
def initialize(#{@inputs_variables.keys.join(':, ')}:)
|
72
|
-
#{@inputs_variables.keys.map { |key| "@#{key} = #{key}" }.join('; ')}
|
70
|
+
def initialize(#{@inputs_variables.keys.join(':, ')}:)
|
71
|
+
#{@inputs_variables.keys.map { |key| "@#{key} = #{key}" }.join('; ')}
|
73
72
|
end
|
74
73
|
RUBY
|
75
74
|
end
|
76
75
|
|
77
76
|
# EXAMPLE:
|
78
77
|
#
|
79
|
-
# private attr_reader(*[:attr_1]);
|
78
|
+
# private; attr_reader(*[:attr_1]);
|
80
79
|
#
|
81
80
|
def context_internal_variables_template
|
82
81
|
return if @internal_variables.blank?
|
data/lib/servactory/inputs.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
class Inputs
|
5
|
-
|
4
|
+
class Inputs
|
5
|
+
def initialize(**)
|
6
|
+
# NOTE: Look at the file `lib/servactory/input_arguments/tools/prepare.rb`
|
7
|
+
end
|
6
8
|
end
|
7
9
|
end
|
data/lib/servactory/version.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: servactory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.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-05-
|
11
|
+
date: 2023-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
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: '
|
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
|