servactory 1.5.2 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +87 -50
- data/config/locales/en.yml +5 -5
- data/lib/servactory/base.rb +4 -4
- data/lib/servactory/configuration/factory.rb +6 -6
- data/lib/servactory/configuration/setup.rb +6 -6
- data/lib/servactory/context/callable.rb +21 -22
- data/lib/servactory/context/configuration.rb +6 -6
- data/lib/servactory/context/workspace/inputs.rb +13 -0
- data/lib/servactory/context/workspace.rb +9 -7
- data/lib/servactory/errors/base.rb +2 -1
- data/lib/servactory/errors/collection.rb +25 -0
- data/lib/servactory/errors/failure.rb +11 -1
- data/lib/servactory/errors/input_error.rb +17 -0
- data/lib/servactory/errors/internal_error.rb +15 -0
- data/lib/servactory/errors/output_error.rb +15 -0
- data/lib/servactory/{input_arguments → inputs}/checks/base.rb +3 -3
- data/lib/servactory/{input_arguments → inputs}/checks/errors.rb +1 -1
- data/lib/servactory/{input_arguments → inputs}/checks/inclusion.rb +2 -2
- data/lib/servactory/{input_arguments → inputs}/checks/must.rb +3 -3
- data/lib/servactory/{input_arguments → inputs}/checks/required.rb +2 -2
- data/lib/servactory/{input_arguments → inputs}/checks/type.rb +3 -3
- data/lib/servactory/{input_arguments → inputs}/collection.rb +1 -1
- data/lib/servactory/{input_arguments → inputs}/define_input_conflict.rb +1 -1
- data/lib/servactory/{input_arguments → inputs}/define_input_method.rb +1 -1
- data/lib/servactory/inputs/dsl.rb +36 -0
- data/lib/servactory/{input_arguments/input_argument.rb → inputs/input.rb} +12 -12
- data/lib/servactory/{input_arguments → inputs}/option.rb +5 -3
- data/lib/servactory/{input_arguments → inputs}/options_collection.rb +1 -1
- data/lib/servactory/{input_arguments → inputs}/tools/check.rb +5 -5
- data/lib/servactory/{input_arguments → inputs}/tools/check_errors.rb +1 -1
- data/lib/servactory/{input_arguments → inputs}/tools/find_unnecessary.rb +6 -6
- data/lib/servactory/{input_arguments → inputs}/tools/prepare.rb +5 -5
- data/lib/servactory/inputs/tools/rules.rb +46 -0
- data/lib/servactory/{input_arguments → inputs}/workbench.rb +8 -8
- data/lib/servactory/internals/checks/base.rb +17 -0
- data/lib/servactory/{internal_arguments → internals}/checks/type.rb +8 -8
- data/lib/servactory/{output_arguments → internals}/collection.rb +1 -1
- data/lib/servactory/internals/dsl.rb +33 -0
- data/lib/servactory/{internal_arguments/internal_argument.rb → internals/internal.rb} +2 -2
- data/lib/servactory/internals/tools/prepare.rb +60 -0
- data/lib/servactory/{internal_arguments → internals}/workbench.rb +5 -5
- data/lib/servactory/{make_methods → methods}/collection.rb +1 -1
- data/lib/servactory/methods/dsl.rb +33 -0
- data/lib/servactory/{make_methods/make_method.rb → methods/method.rb} +2 -2
- data/lib/servactory/{make_methods → methods}/workbench.rb +5 -5
- data/lib/servactory/outputs/checks/base.rb +17 -0
- data/lib/servactory/{output_arguments → outputs}/checks/type.rb +8 -8
- data/lib/servactory/{internal_arguments → outputs}/collection.rb +1 -1
- data/lib/servactory/outputs/dsl.rb +33 -0
- data/lib/servactory/{output_arguments/output_argument.rb → outputs/output.rb} +2 -2
- data/lib/servactory/{output_arguments → outputs}/tools/conflicts.rb +7 -7
- data/lib/servactory/outputs/tools/prepare.rb +62 -0
- data/lib/servactory/outputs/workbench.rb +31 -0
- data/lib/servactory/result.rb +4 -4
- data/lib/servactory/utils.rb +2 -0
- data/lib/servactory/version.rb +2 -2
- metadata +59 -46
- data/lib/servactory/context/workspace/error.rb +0 -19
- data/lib/servactory/context/workspace/errors.rb +0 -33
- data/lib/servactory/errors/input_argument_error.rb +0 -7
- data/lib/servactory/errors/internal_argument_error.rb +0 -7
- data/lib/servactory/errors/output_argument_error.rb +0 -7
- data/lib/servactory/input_arguments/dsl.rb +0 -36
- data/lib/servactory/input_arguments/tools/rules.rb +0 -43
- data/lib/servactory/inputs.rb +0 -9
- data/lib/servactory/internal_arguments/checks/base.rb +0 -17
- data/lib/servactory/internal_arguments/dsl.rb +0 -33
- data/lib/servactory/internal_arguments/tools/prepare.rb +0 -60
- data/lib/servactory/make_methods/dsl.rb +0 -33
- data/lib/servactory/output_arguments/checks/base.rb +0 -17
- data/lib/servactory/output_arguments/dsl.rb +0 -33
- data/lib/servactory/output_arguments/tools/prepare.rb +0 -62
- data/lib/servactory/output_arguments/workbench.rb +0 -31
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Checks
|
6
6
|
class Must < Base
|
7
7
|
DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:, code:|
|
8
8
|
I18n.t(
|
9
|
-
"servactory.
|
9
|
+
"servactory.inputs.checks.must.default_error",
|
10
10
|
service_class_name: service_class_name,
|
11
11
|
input_name: input.name,
|
12
12
|
value: value,
|
@@ -16,7 +16,7 @@ module Servactory
|
|
16
16
|
|
17
17
|
SYNTAX_ERROR_MESSAGE = lambda do |service_class_name:, input:, value:, code:, exception_message:|
|
18
18
|
I18n.t(
|
19
|
-
"servactory.
|
19
|
+
"servactory.inputs.checks.must.syntax_error",
|
20
20
|
service_class_name: service_class_name,
|
21
21
|
input_name: input.name,
|
22
22
|
value: value,
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Checks
|
6
6
|
class Required < Base
|
7
7
|
DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
|
8
|
-
i18n_key = "servactory.
|
8
|
+
i18n_key = "servactory.inputs.checks.required.default_error."
|
9
9
|
i18n_key += input.array? && value.present? ? "for_array" : "default"
|
10
10
|
|
11
11
|
I18n.t(
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Checks
|
6
6
|
class Type < Base
|
7
7
|
DEFAULT_MESSAGE = lambda do |service_class_name:, input:, expected_type:, given_type:|
|
@@ -14,7 +14,7 @@ module Servactory
|
|
14
14
|
array_message
|
15
15
|
else
|
16
16
|
I18n.t(
|
17
|
-
"servactory.
|
17
|
+
"servactory.inputs.checks.type.default_error.for_array",
|
18
18
|
service_class_name: service_class_name,
|
19
19
|
input_name: input.name,
|
20
20
|
expected_type: expected_type,
|
@@ -23,7 +23,7 @@ module Servactory
|
|
23
23
|
end
|
24
24
|
else
|
25
25
|
I18n.t(
|
26
|
-
"servactory.
|
26
|
+
"servactory.inputs.checks.type.default_error.default",
|
27
27
|
service_class_name: service_class_name,
|
28
28
|
input_name: input.name,
|
29
29
|
expected_type: expected_type,
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Inputs
|
5
|
+
module DSL
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def inherited(child)
|
12
|
+
super
|
13
|
+
|
14
|
+
child.send(:collection_of_inputs).merge(collection_of_inputs)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def input(name, **options)
|
20
|
+
collection_of_inputs << Input.new(
|
21
|
+
name,
|
22
|
+
**options
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def collection_of_inputs
|
27
|
+
@collection_of_inputs ||= Collection.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def inputs_workbench
|
31
|
+
@inputs_workbench ||= Workbench.work_with(collection_of_inputs)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
5
|
-
class
|
4
|
+
module Inputs
|
5
|
+
class Input # rubocop:disable Metrics/ClassLength
|
6
6
|
ARRAY_DEFAULT_VALUE = ->(is: false, message: nil) { { is: is, message: message } }
|
7
7
|
|
8
8
|
attr_reader :name,
|
@@ -29,18 +29,18 @@ module Servactory
|
|
29
29
|
# rubocop:enable Style/KeywordParametersOrder
|
30
30
|
|
31
31
|
def add_basic_options_with(type:, options:)
|
32
|
-
# Check Class: Servactory::
|
32
|
+
# Check Class: Servactory::Inputs::Checks::Required
|
33
33
|
add_required_option_with(options)
|
34
34
|
|
35
|
-
# Check Class: Servactory::
|
35
|
+
# Check Class: Servactory::Inputs::Checks::Type
|
36
36
|
add_types_option_with(type)
|
37
37
|
add_default_option_with(options)
|
38
38
|
add_array_option_with(options)
|
39
39
|
|
40
|
-
# Check Class: Servactory::
|
40
|
+
# Check Class: Servactory::Inputs::Checks::Inclusion
|
41
41
|
add_inclusion_option_with(options)
|
42
42
|
|
43
|
-
# Check Class: Servactory::
|
43
|
+
# Check Class: Servactory::Inputs::Checks::Must
|
44
44
|
add_must_option_with(options)
|
45
45
|
|
46
46
|
# Check Class: nil
|
@@ -51,7 +51,7 @@ module Servactory
|
|
51
51
|
collection_of_options << Option.new(
|
52
52
|
name: :required,
|
53
53
|
input: self,
|
54
|
-
check_class: Servactory::
|
54
|
+
check_class: Servactory::Inputs::Checks::Required,
|
55
55
|
define_input_methods: [
|
56
56
|
DefineInputMethod.new(
|
57
57
|
name: :required?,
|
@@ -76,7 +76,7 @@ module Servactory
|
|
76
76
|
collection_of_options << Option.new(
|
77
77
|
name: :types,
|
78
78
|
input: self,
|
79
|
-
check_class: Servactory::
|
79
|
+
check_class: Servactory::Inputs::Checks::Type,
|
80
80
|
original_value: Array(type),
|
81
81
|
need_for_checks: true,
|
82
82
|
value_fallback: nil,
|
@@ -88,7 +88,7 @@ module Servactory
|
|
88
88
|
collection_of_options << Option.new(
|
89
89
|
name: :default,
|
90
90
|
input: self,
|
91
|
-
check_class: Servactory::
|
91
|
+
check_class: Servactory::Inputs::Checks::Type,
|
92
92
|
define_input_methods: [
|
93
93
|
DefineInputMethod.new(
|
94
94
|
name: :default_value_present?,
|
@@ -106,7 +106,7 @@ module Servactory
|
|
106
106
|
collection_of_options << Option.new(
|
107
107
|
name: :array,
|
108
108
|
input: self,
|
109
|
-
check_class: Servactory::
|
109
|
+
check_class: Servactory::Inputs::Checks::Type,
|
110
110
|
define_input_methods: [
|
111
111
|
DefineInputMethod.new(
|
112
112
|
name: :array?,
|
@@ -128,7 +128,7 @@ module Servactory
|
|
128
128
|
collection_of_options << Option.new(
|
129
129
|
name: :inclusion,
|
130
130
|
input: self,
|
131
|
-
check_class: Servactory::
|
131
|
+
check_class: Servactory::Inputs::Checks::Inclusion,
|
132
132
|
define_input_methods: [
|
133
133
|
DefineInputMethod.new(
|
134
134
|
name: :inclusion_present?,
|
@@ -146,7 +146,7 @@ module Servactory
|
|
146
146
|
collection_of_options << Option.new(
|
147
147
|
name: :must,
|
148
148
|
input: self,
|
149
|
-
check_class: Servactory::
|
149
|
+
check_class: Servactory::Inputs::Checks::Must,
|
150
150
|
define_input_methods: [
|
151
151
|
DefineInputMethod.new(
|
152
152
|
name: :must_present?,
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
class Option
|
6
6
|
DEFAULT_VALUE = ->(key:, value:, message: nil) { { key => value, message: message } }
|
7
7
|
|
@@ -66,10 +66,12 @@ module Servactory
|
|
66
66
|
|
67
67
|
def prepare_advanced_for(value:, value_fallback:)
|
68
68
|
if value.is_a?(Hash)
|
69
|
+
message = value.fetch(:message, nil)
|
70
|
+
|
69
71
|
DEFAULT_VALUE.call(
|
70
72
|
key: value_key,
|
71
|
-
value: value.fetch(value_key, value_fallback),
|
72
|
-
message:
|
73
|
+
value: value.fetch(value_key, message.present? ? true : value_fallback),
|
74
|
+
message: message
|
73
75
|
)
|
74
76
|
else
|
75
77
|
DEFAULT_VALUE.call(key: value_key, value: value)
|
@@ -1,21 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Tools
|
6
6
|
class Check
|
7
7
|
def self.check!(...)
|
8
8
|
new(...).check!
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(context, incoming_arguments,
|
11
|
+
def initialize(context, incoming_arguments, collection_of_inputs)
|
12
12
|
@context = context
|
13
13
|
@incoming_arguments = incoming_arguments
|
14
|
-
@
|
14
|
+
@collection_of_inputs = collection_of_inputs
|
15
15
|
end
|
16
16
|
|
17
17
|
def check!
|
18
|
-
@
|
18
|
+
@collection_of_inputs.each do |input|
|
19
19
|
process_input(input)
|
20
20
|
end
|
21
21
|
|
@@ -68,7 +68,7 @@ module Servactory
|
|
68
68
|
def raise_errors
|
69
69
|
return if (tmp_errors = errors.not_blank).empty?
|
70
70
|
|
71
|
-
raise Servactory.configuration.
|
71
|
+
raise Servactory.configuration.input_error_class.new(message: tmp_errors.first)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -1,35 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Tools
|
6
6
|
class FindUnnecessary
|
7
7
|
def self.check!(...)
|
8
8
|
new(...).check!
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(context, incoming_arguments,
|
11
|
+
def initialize(context, incoming_arguments, collection_of_inputs)
|
12
12
|
@context = context
|
13
13
|
@incoming_arguments = incoming_arguments
|
14
|
-
@
|
14
|
+
@collection_of_inputs = collection_of_inputs
|
15
15
|
end
|
16
16
|
|
17
17
|
def check!
|
18
18
|
return if unnecessary_attributes.empty?
|
19
19
|
|
20
20
|
message_text = I18n.t(
|
21
|
-
"servactory.
|
21
|
+
"servactory.inputs.tools.find_unnecessary.error",
|
22
22
|
service_class_name: @context.class.name,
|
23
23
|
unnecessary_attributes: unnecessary_attributes.join(", ")
|
24
24
|
)
|
25
25
|
|
26
|
-
raise Servactory.configuration.
|
26
|
+
raise Servactory.configuration.input_error_class.new(message: message_text)
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def unnecessary_attributes
|
32
|
-
@unnecessary_attributes ||= @incoming_arguments.keys - @
|
32
|
+
@unnecessary_attributes ||= @incoming_arguments.keys - @collection_of_inputs.names
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -1,24 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
module Tools
|
6
6
|
class Prepare
|
7
7
|
def self.prepare(...)
|
8
8
|
new(...).prepare
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(context, incoming_arguments,
|
11
|
+
def initialize(context, incoming_arguments, collection_of_inputs)
|
12
12
|
@context = context
|
13
13
|
@incoming_arguments = incoming_arguments
|
14
|
-
@
|
14
|
+
@collection_of_inputs = collection_of_inputs
|
15
15
|
end
|
16
16
|
|
17
17
|
def prepare
|
18
18
|
@inputs_variables = {}
|
19
19
|
@internal_variables = {}
|
20
20
|
|
21
|
-
@
|
21
|
+
@collection_of_inputs.each do |input|
|
22
22
|
process_input(input)
|
23
23
|
end
|
24
24
|
|
@@ -39,7 +39,7 @@ module Servactory
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def create_instance_variables
|
42
|
-
inputs_class = Servactory::Inputs.dup
|
42
|
+
inputs_class = Servactory::Context::Workspace::Inputs.dup
|
43
43
|
inputs_class.class_eval(class_inputs_template) if class_inputs_template.present?
|
44
44
|
|
45
45
|
@context.assign_inputs(inputs_class.new(**@inputs_variables))
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Inputs
|
5
|
+
module Tools
|
6
|
+
class Rules
|
7
|
+
def self.check!(...)
|
8
|
+
new(...).check!
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(context, collection_of_inputs)
|
12
|
+
@context = context
|
13
|
+
@collection_of_inputs = collection_of_inputs
|
14
|
+
end
|
15
|
+
|
16
|
+
def check!
|
17
|
+
@collection_of_inputs.each do |input|
|
18
|
+
check_for!(input)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def check_for!(input)
|
25
|
+
return unless input.with_conflicts?
|
26
|
+
|
27
|
+
raise_error_for(input)
|
28
|
+
end
|
29
|
+
|
30
|
+
def raise_error_for(input)
|
31
|
+
message_text = I18n.t(
|
32
|
+
"servactory.inputs.tools.rules.error",
|
33
|
+
service_class_name: @context.class.name,
|
34
|
+
input_name: input.name,
|
35
|
+
conflict_code: input.conflict_code
|
36
|
+
)
|
37
|
+
|
38
|
+
raise Servactory.configuration.input_error_class.new(
|
39
|
+
message: message_text,
|
40
|
+
input_name: input.name
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Inputs
|
5
5
|
class Workbench
|
6
6
|
def self.work_with(...)
|
7
7
|
new(...)
|
8
8
|
end
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
@
|
10
|
+
def initialize(collection_of_inputs)
|
11
|
+
@collection_of_inputs = collection_of_inputs
|
12
12
|
end
|
13
13
|
|
14
14
|
def assign(context:, arguments:)
|
@@ -17,25 +17,25 @@ module Servactory
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def find_unnecessary!
|
20
|
-
Tools::FindUnnecessary.check!(context, @incoming_arguments,
|
20
|
+
Tools::FindUnnecessary.check!(context, @incoming_arguments, collection_of_inputs)
|
21
21
|
end
|
22
22
|
|
23
23
|
def check_rules!
|
24
|
-
Tools::Rules.check!(context,
|
24
|
+
Tools::Rules.check!(context, collection_of_inputs)
|
25
25
|
end
|
26
26
|
|
27
27
|
def prepare
|
28
|
-
Tools::Prepare.prepare(context, @incoming_arguments,
|
28
|
+
Tools::Prepare.prepare(context, @incoming_arguments, collection_of_inputs)
|
29
29
|
end
|
30
30
|
|
31
31
|
def check!
|
32
|
-
Tools::Check.check!(context, @incoming_arguments,
|
32
|
+
Tools::Check.check!(context, @incoming_arguments, collection_of_inputs)
|
33
33
|
end
|
34
34
|
|
35
35
|
private
|
36
36
|
|
37
37
|
attr_reader :context,
|
38
|
-
:
|
38
|
+
:collection_of_inputs
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Internals
|
5
|
+
module Checks
|
6
|
+
class Base
|
7
|
+
protected
|
8
|
+
|
9
|
+
def raise_error_with(message, **attributes)
|
10
|
+
message = message.call(**attributes) if message.is_a?(Proc)
|
11
|
+
|
12
|
+
raise Servactory.configuration.internal_error_class.new(message: message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Servactory
|
4
|
-
module
|
4
|
+
module Internals
|
5
5
|
module Checks
|
6
6
|
class Type < Base
|
7
|
-
DEFAULT_MESSAGE = lambda do |service_class_name:,
|
7
|
+
DEFAULT_MESSAGE = lambda do |service_class_name:, internal:, expected_type:, given_type:|
|
8
8
|
I18n.t(
|
9
|
-
"servactory.
|
9
|
+
"servactory.internals.checks.type.default_error",
|
10
10
|
service_class_name: service_class_name,
|
11
|
-
|
11
|
+
internal_name: internal.name,
|
12
12
|
expected_type: expected_type,
|
13
13
|
given_type: given_type
|
14
14
|
)
|
@@ -22,11 +22,11 @@ module Servactory
|
|
22
22
|
|
23
23
|
##########################################################################
|
24
24
|
|
25
|
-
def initialize(context:,
|
25
|
+
def initialize(context:, internal:, value:)
|
26
26
|
super()
|
27
27
|
|
28
28
|
@context = context
|
29
|
-
@
|
29
|
+
@internal = internal
|
30
30
|
@value = value
|
31
31
|
end
|
32
32
|
|
@@ -36,7 +36,7 @@ module Servactory
|
|
36
36
|
raise_error_with(
|
37
37
|
DEFAULT_MESSAGE,
|
38
38
|
service_class_name: @context.class.name,
|
39
|
-
|
39
|
+
internal: @internal,
|
40
40
|
expected_type: prepared_types.join(", "),
|
41
41
|
given_type: @value.class.name
|
42
42
|
)
|
@@ -46,7 +46,7 @@ module Servactory
|
|
46
46
|
|
47
47
|
def prepared_types
|
48
48
|
@prepared_types ||=
|
49
|
-
Array(@
|
49
|
+
Array(@internal.types).map do |type|
|
50
50
|
if type.is_a?(String)
|
51
51
|
Object.const_get(type)
|
52
52
|
else
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Internals
|
5
|
+
module DSL
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def inherited(child)
|
12
|
+
super
|
13
|
+
|
14
|
+
child.send(:collection_of_internals).merge(collection_of_internals)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def internal(name, **options)
|
20
|
+
collection_of_internals << Internal.new(name, **options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection_of_internals
|
24
|
+
@collection_of_internals ||= Collection.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def internals_workbench
|
28
|
+
@internals_workbench ||= Workbench.work_with(collection_of_internals)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Internals
|
5
|
+
module Tools
|
6
|
+
class Prepare
|
7
|
+
def self.prepare(...)
|
8
|
+
new(...).prepare
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(context, collection_of_internals)
|
12
|
+
@context = context
|
13
|
+
@collection_of_internals = collection_of_internals
|
14
|
+
end
|
15
|
+
|
16
|
+
def prepare
|
17
|
+
@collection_of_internals.each do |internal|
|
18
|
+
create_instance_variable_for(internal)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def create_instance_variable_for(internal)
|
25
|
+
@context.instance_variable_set(:"@#{internal.name}", nil)
|
26
|
+
|
27
|
+
@context.class.class_eval(context_internal_template_for(internal))
|
28
|
+
end
|
29
|
+
|
30
|
+
# EXAMPLE:
|
31
|
+
#
|
32
|
+
# define_method(:user=) do |value|
|
33
|
+
# Servactory::Internals::Checks::Type.check!( context: self, internal:, value: )
|
34
|
+
#
|
35
|
+
# instance_variable_set(:@user, value)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# private attr_reader :user
|
39
|
+
#
|
40
|
+
def context_internal_template_for(internal)
|
41
|
+
<<-RUBY
|
42
|
+
define_method(:#{internal.name}=) do |value|
|
43
|
+
Servactory::Internals::Checks::Type.check!(
|
44
|
+
context: self,
|
45
|
+
internal: internal,
|
46
|
+
value: value
|
47
|
+
)
|
48
|
+
|
49
|
+
instance_variable_set(:@#{internal.name}, value)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_reader :#{internal.name}
|
55
|
+
RUBY
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|