smart_initializer 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +35 -3
- data/Gemfile.lock +38 -38
- data/README.md +260 -46
- data/bin/console +2 -2
- data/gemfiles/with_external_deps.gemfile.lock +38 -38
- data/lib/smart_core/initializer/attribute/factory/base.rb +145 -0
- data/lib/smart_core/initializer/attribute/factory/option.rb +107 -0
- data/lib/smart_core/initializer/attribute/factory/param.rb +63 -0
- data/lib/smart_core/initializer/attribute/factory.rb +5 -199
- data/lib/smart_core/initializer/attribute/finalizer/abstract.rb +2 -0
- data/lib/smart_core/initializer/attribute/finalizer/instance_method.rb +1 -1
- data/lib/smart_core/initializer/attribute/finalizer.rb +5 -5
- data/lib/smart_core/initializer/attribute/list.rb +20 -0
- data/lib/smart_core/initializer/attribute/{parameters.rb → value/base.rb} +35 -65
- data/lib/smart_core/initializer/attribute/value/option.rb +101 -0
- data/lib/smart_core/initializer/attribute/value/param.rb +24 -0
- data/lib/smart_core/initializer/attribute/value.rb +9 -0
- data/lib/smart_core/initializer/attribute.rb +3 -125
- data/lib/smart_core/initializer/configuration.rb +7 -3
- data/lib/smart_core/initializer/constructor/definer.rb +135 -46
- data/lib/smart_core/initializer/constructor.rb +30 -12
- data/lib/smart_core/initializer/dsl.rb +38 -24
- data/lib/smart_core/initializer/errors.rb +20 -4
- data/lib/smart_core/initializer/functionality.rb +7 -8
- data/lib/smart_core/initializer/settings/auto_cast.rb +40 -0
- data/lib/smart_core/initializer/settings/base.rb +49 -0
- data/lib/smart_core/initializer/settings/duplicator.rb +5 -0
- data/lib/smart_core/initializer/settings/strict_options.rb +40 -0
- data/lib/smart_core/initializer/settings/type_system.rb +10 -28
- data/lib/smart_core/initializer/settings.rb +40 -0
- data/lib/smart_core/initializer/type_system/registry.rb +2 -1
- data/lib/smart_core/initializer/type_system/smart_types.rb +2 -2
- data/lib/smart_core/initializer/version.rb +2 -2
- data/lib/smart_core/initializer.rb +14 -4
- data/smart_initializer.gemspec +2 -2
- metadata +16 -7
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
smart_initializer (0.
|
4
|
+
smart_initializer (0.8.0)
|
5
5
|
qonfig (~> 0.24)
|
6
6
|
smart_engine (~> 0.11)
|
7
7
|
smart_types (~> 0.4)
|
@@ -9,39 +9,39 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activesupport (6.1.1)
|
12
|
+
activesupport (6.1.4.1)
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
14
|
i18n (>= 1.6, < 2)
|
15
15
|
minitest (>= 5.1)
|
16
16
|
tzinfo (~> 2.0)
|
17
17
|
zeitwerk (~> 2.3)
|
18
|
-
armitage-rubocop (1.
|
19
|
-
rubocop (= 1.
|
20
|
-
rubocop-performance (= 1.
|
21
|
-
rubocop-rails (= 2.
|
22
|
-
rubocop-rake (= 0.
|
23
|
-
rubocop-rspec (= 2.
|
24
|
-
ast (2.4.
|
18
|
+
armitage-rubocop (1.23.0.1)
|
19
|
+
rubocop (= 1.23.0)
|
20
|
+
rubocop-performance (= 1.12.0)
|
21
|
+
rubocop-rails (= 2.12.4)
|
22
|
+
rubocop-rake (= 0.6.0)
|
23
|
+
rubocop-rspec (= 2.6.0)
|
24
|
+
ast (2.4.2)
|
25
25
|
coderay (1.1.3)
|
26
|
-
concurrent-ruby (1.1.
|
26
|
+
concurrent-ruby (1.1.9)
|
27
27
|
diff-lcs (1.4.4)
|
28
28
|
docile (1.3.5)
|
29
|
-
i18n (1.8.
|
29
|
+
i18n (1.8.11)
|
30
30
|
concurrent-ruby (~> 1.0)
|
31
31
|
method_source (1.0.0)
|
32
|
-
minitest (5.14.
|
33
|
-
parallel (1.
|
34
|
-
parser (3.0.
|
32
|
+
minitest (5.14.4)
|
33
|
+
parallel (1.21.0)
|
34
|
+
parser (3.0.3.1)
|
35
35
|
ast (~> 2.4.1)
|
36
|
-
pry (0.
|
36
|
+
pry (0.14.1)
|
37
37
|
coderay (~> 1.1)
|
38
38
|
method_source (~> 1.0)
|
39
|
-
qonfig (0.
|
39
|
+
qonfig (0.26.0)
|
40
40
|
rack (2.2.3)
|
41
41
|
rainbow (3.0.0)
|
42
42
|
rake (13.0.3)
|
43
|
-
regexp_parser (2.
|
44
|
-
rexml (3.2.
|
43
|
+
regexp_parser (2.1.1)
|
44
|
+
rexml (3.2.5)
|
45
45
|
rspec (3.10.0)
|
46
46
|
rspec-core (~> 3.10.0)
|
47
47
|
rspec-expectations (~> 3.10.0)
|
@@ -55,29 +55,28 @@ GEM
|
|
55
55
|
diff-lcs (>= 1.2.0, < 2.0)
|
56
56
|
rspec-support (~> 3.10.0)
|
57
57
|
rspec-support (3.10.1)
|
58
|
-
rubocop (1.
|
58
|
+
rubocop (1.23.0)
|
59
59
|
parallel (~> 1.10)
|
60
|
-
parser (>=
|
60
|
+
parser (>= 3.0.0.0)
|
61
61
|
rainbow (>= 2.2.2, < 4.0)
|
62
62
|
regexp_parser (>= 1.8, < 3.0)
|
63
63
|
rexml
|
64
|
-
rubocop-ast (>= 1.
|
64
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
65
65
|
ruby-progressbar (~> 1.7)
|
66
|
-
unicode-display_width (>= 1.4.0, <
|
67
|
-
rubocop-ast (1.
|
68
|
-
parser (>=
|
69
|
-
rubocop-performance (1.
|
70
|
-
rubocop (>=
|
66
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
67
|
+
rubocop-ast (1.13.0)
|
68
|
+
parser (>= 3.0.1.1)
|
69
|
+
rubocop-performance (1.12.0)
|
70
|
+
rubocop (>= 1.7.0, < 2.0)
|
71
71
|
rubocop-ast (>= 0.4.0)
|
72
|
-
rubocop-rails (2.
|
72
|
+
rubocop-rails (2.12.4)
|
73
73
|
activesupport (>= 4.2.0)
|
74
74
|
rack (>= 1.1)
|
75
|
-
rubocop (>=
|
76
|
-
rubocop-rake (0.
|
77
|
-
rubocop
|
78
|
-
rubocop-rspec (2.1.0)
|
75
|
+
rubocop (>= 1.7.0, < 2.0)
|
76
|
+
rubocop-rake (0.6.0)
|
79
77
|
rubocop (~> 1.0)
|
80
|
-
|
78
|
+
rubocop-rspec (2.6.0)
|
79
|
+
rubocop (~> 1.19)
|
81
80
|
ruby-progressbar (1.11.0)
|
82
81
|
simplecov (0.21.2)
|
83
82
|
docile (~> 1.1)
|
@@ -86,21 +85,22 @@ GEM
|
|
86
85
|
simplecov-html (0.12.3)
|
87
86
|
simplecov_json_formatter (0.1.2)
|
88
87
|
smart_engine (0.11.0)
|
89
|
-
smart_types (0.
|
88
|
+
smart_types (0.7.0)
|
90
89
|
smart_engine (~> 0.11)
|
91
90
|
thy (0.1.4)
|
92
91
|
tzinfo (2.0.4)
|
93
92
|
concurrent-ruby (~> 1.0)
|
94
|
-
unicode-display_width (1.
|
95
|
-
zeitwerk (2.
|
93
|
+
unicode-display_width (2.1.0)
|
94
|
+
zeitwerk (2.5.1)
|
96
95
|
|
97
96
|
PLATFORMS
|
98
97
|
x86_64-darwin-20
|
98
|
+
x86_64-darwin-21
|
99
99
|
|
100
100
|
DEPENDENCIES
|
101
|
-
armitage-rubocop (~> 1.
|
101
|
+
armitage-rubocop (~> 1.23)
|
102
102
|
bundler (~> 2.2)
|
103
|
-
pry (~> 0.
|
103
|
+
pry (~> 0.14)
|
104
104
|
rake (~> 13.0)
|
105
105
|
rspec (~> 3.10)
|
106
106
|
simplecov (~> 0.21)
|
@@ -108,4 +108,4 @@ DEPENDENCIES
|
|
108
108
|
thy (~> 0.1.4)
|
109
109
|
|
110
110
|
BUNDLED WITH
|
111
|
-
2.2.
|
111
|
+
2.2.31
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.8.0
|
5
|
+
class SmartCore::Initializer::Attribute::Factory::Base
|
6
|
+
class << self
|
7
|
+
# @!method create(*params)
|
8
|
+
# @param params [Any] List of appropriated attributes for the corresponding factory
|
9
|
+
# @return [SmartCore::Initializer::Attribute::Base]
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# @param name [String, Symbol]
|
14
|
+
# @return [Symbol]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
# @since 0.8.0
|
18
|
+
def prepare_name_param(name)
|
19
|
+
unless name.is_a?(::String) || name.is_a?(::Symbol)
|
20
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
21
|
+
Attribute name should be a type of String or Symbol
|
22
|
+
ERROR_MESSAGE
|
23
|
+
end
|
24
|
+
|
25
|
+
name.to_sym
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param type [String, Symbol, Any]
|
29
|
+
# @param type_system [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
30
|
+
# @return [SmartCore::Initializer::TypeSystem::Interop]
|
31
|
+
#
|
32
|
+
# @api private
|
33
|
+
# @since 0.8.0
|
34
|
+
def prepare_type_param(type, type_system)
|
35
|
+
type_primitive =
|
36
|
+
if type.is_a?(::String) || type.is_a?(::Symbol)
|
37
|
+
type_system.type_from_alias(type)
|
38
|
+
else
|
39
|
+
type
|
40
|
+
end
|
41
|
+
|
42
|
+
type_system.create(type_primitive)
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param cast [Boolean]
|
46
|
+
# @return [Boolean]
|
47
|
+
#
|
48
|
+
# @api private
|
49
|
+
# @since 0.8.0
|
50
|
+
def prepare_cast_param(cast)
|
51
|
+
unless cast.is_a?(::TrueClass) || cast.is_a?(::FalseClass)
|
52
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
53
|
+
Attribute cast should be a type of boolean
|
54
|
+
ERROR_MESSAGE
|
55
|
+
end
|
56
|
+
|
57
|
+
cast
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param privacy [String, Symbol]
|
61
|
+
# @return [Symbol]
|
62
|
+
#
|
63
|
+
# @api private
|
64
|
+
# @since 0.8.0
|
65
|
+
def prepare_privacy_param(privacy)
|
66
|
+
unless privacy.is_a?(::String) || privacy.is_a?(::Symbol)
|
67
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
68
|
+
Attribute privacy should be a type of String or Symbol
|
69
|
+
ERROR_MESSAGE
|
70
|
+
end
|
71
|
+
|
72
|
+
SmartCore::Initializer::Attribute::Value::Base::PRIVACY_MODES.fetch(privacy.to_sym) do
|
73
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
74
|
+
Incorrect attribute privacy identifier "#{privacy}"
|
75
|
+
ERROR_MESSAGE
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param finalize [String, Symbol, Proc]
|
80
|
+
# @return [SmartCore::Initializer::Attribute::Finalizer::AnonymousBlock/InstanceMethod]
|
81
|
+
#
|
82
|
+
# @api private
|
83
|
+
# @since 0.8.0
|
84
|
+
def prepare_finalize_param(finalize)
|
85
|
+
unless finalize.is_a?(::String) || finalize.is_a?(::Symbol) || finalize.is_a?(::Proc)
|
86
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
87
|
+
Attribute finalizer should be a type of String, Symbol or Proc
|
88
|
+
ERROR_MESSAGE
|
89
|
+
end
|
90
|
+
|
91
|
+
# rubocop:disable Style/SoleNestedConditional
|
92
|
+
if finalize.is_a?(::Proc) && finalize.lambda?
|
93
|
+
unless (finalize.arity == 1 || finalize.arity == -1)
|
94
|
+
raise(
|
95
|
+
SmartCore::Initializer::ArgumentError,
|
96
|
+
'Lambda-based finalizer should have arity equal to 1 or equal to -1 ' \
|
97
|
+
'(your lambda object should require one attribute)'
|
98
|
+
) # TODO: show the name of attribute in error message (if the name is a valid, of course)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
# rubocop:enable Style/SoleNestedConditional
|
102
|
+
|
103
|
+
SmartCore::Initializer::Attribute::Finalizer.create(finalize)
|
104
|
+
end
|
105
|
+
|
106
|
+
# @param mutable [Boolean]
|
107
|
+
# @return [Boolean]
|
108
|
+
#
|
109
|
+
# @api private
|
110
|
+
# @since 0.4.0
|
111
|
+
def prepare_mutable_param(mutable)
|
112
|
+
unless mutable.is_a?(::FalseClass) || mutable.is_a?(::TrueClass)
|
113
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
114
|
+
:mutable attribute should be a type of boolean
|
115
|
+
ERROR_MESSAGE
|
116
|
+
end
|
117
|
+
|
118
|
+
mutable
|
119
|
+
end
|
120
|
+
|
121
|
+
# @param as [String, Symbol, NilClass]
|
122
|
+
# @return [String, Symbol, NilClass]
|
123
|
+
#
|
124
|
+
# @api private
|
125
|
+
# @since 0.4.0
|
126
|
+
def preapre_as_param(as)
|
127
|
+
unless as.is_a?(::NilClass) || as.is_a?(::String) || as.is_a?(::Symbol)
|
128
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
129
|
+
Attribute alias should be a type of String or Symbol
|
130
|
+
ERROR_MESSAGE
|
131
|
+
end
|
132
|
+
|
133
|
+
as
|
134
|
+
end
|
135
|
+
|
136
|
+
# @param type_system [String, Symbol]
|
137
|
+
# @return [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
138
|
+
#
|
139
|
+
# @api private
|
140
|
+
# @since 0.8.0
|
141
|
+
def prepare_type_system_param(type_system)
|
142
|
+
SmartCore::Initializer::TypeSystem.resolve(type_system)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SmartCore::Initializer::Attribute::Factory
|
4
|
+
# @api private
|
5
|
+
# @since 0.8.0
|
6
|
+
class Option < Base
|
7
|
+
class << self
|
8
|
+
# @param name [String, Symbol]
|
9
|
+
# @param type [String, Symbol, Any]
|
10
|
+
# @param type_system [String, Symbol]
|
11
|
+
# @param privacy [String, Symbol]
|
12
|
+
# @param finalize [String, Symbol, Proc]
|
13
|
+
# @param cast [Boolean]
|
14
|
+
# @param mutable [Boolean]
|
15
|
+
# @param as [String, Symbol, NilClass]
|
16
|
+
# @param default [Proc, Any]
|
17
|
+
# @param optional [Boolean]
|
18
|
+
# @return [SmartCore::Initializer::Attribute::Value::Option]
|
19
|
+
#
|
20
|
+
# @api private
|
21
|
+
# @since 0.8.0
|
22
|
+
def create(name, type, type_system, privacy, finalize, cast, mutable, as, default, optional)
|
23
|
+
prepared_name = prepare_name_param(name)
|
24
|
+
prepared_privacy = prepare_privacy_param(privacy)
|
25
|
+
prepared_finalize = prepare_finalize_param(finalize)
|
26
|
+
prepared_cast = prepare_cast_param(cast)
|
27
|
+
prepared_type_system = prepare_type_system_param(type_system)
|
28
|
+
prepared_type = prepare_type_param(type, prepared_type_system)
|
29
|
+
prepared_mutable = prepare_mutable_param(mutable)
|
30
|
+
prepared_as = preapre_as_param(as)
|
31
|
+
prepared_default = prepare_default_param(default)
|
32
|
+
prepared_optional = prepare_optional_param(optional)
|
33
|
+
|
34
|
+
create_attribute(
|
35
|
+
prepared_name,
|
36
|
+
prepared_type,
|
37
|
+
prepared_type_system,
|
38
|
+
prepared_privacy,
|
39
|
+
prepared_finalize,
|
40
|
+
prepared_cast,
|
41
|
+
prepared_mutable,
|
42
|
+
prepared_as,
|
43
|
+
prepared_default,
|
44
|
+
prepared_optional
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# @param name [String]
|
51
|
+
# @param type [SmartCore::Initializer::TypeSystem::Interop]
|
52
|
+
# @param type_system [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
53
|
+
# @param privacy [Symbol]
|
54
|
+
# @param finalize [SmartCore::Initializer::Attribute::Finalizer::Abstract]
|
55
|
+
# @param cast [Boolean]
|
56
|
+
# @param mutable [Boolean]
|
57
|
+
# @param as [String, Symbol]
|
58
|
+
# @param default [Proc, Any]
|
59
|
+
# @param optional [Boolean]
|
60
|
+
# @return [SmartCore::Initializer::Attribute::Value::Option]
|
61
|
+
#
|
62
|
+
# @api private
|
63
|
+
# @since 0.8.0
|
64
|
+
def create_attribute(
|
65
|
+
name,
|
66
|
+
type,
|
67
|
+
type_system,
|
68
|
+
privacy,
|
69
|
+
finalize,
|
70
|
+
cast,
|
71
|
+
mutable,
|
72
|
+
as,
|
73
|
+
default,
|
74
|
+
optional
|
75
|
+
)
|
76
|
+
SmartCore::Initializer::Attribute::Value::Option.new(
|
77
|
+
name, type, type_system, privacy, finalize, cast, mutable, as, default, optional
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param default [Proc, Any]
|
82
|
+
# @return [Proc, Any]
|
83
|
+
#
|
84
|
+
# @api private
|
85
|
+
# @since 0.8.0
|
86
|
+
def prepare_default_param(default)
|
87
|
+
# NOTE: this "strange" approach is used to comply the default Factory methods approach here
|
88
|
+
default
|
89
|
+
end
|
90
|
+
|
91
|
+
# @param optional [Boolean]
|
92
|
+
# @return [Boolean]
|
93
|
+
#
|
94
|
+
# @api private
|
95
|
+
# @since 0.8.0
|
96
|
+
def prepare_optional_param(optional)
|
97
|
+
unless optional.is_a?(::TrueClass) || optional.is_a?(::FalseClass)
|
98
|
+
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
99
|
+
:optional attribute should be a type of boolean
|
100
|
+
ERROR_MESSAGE
|
101
|
+
end
|
102
|
+
|
103
|
+
optional
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SmartCore::Initializer::Attribute::Factory
|
4
|
+
# @api private
|
5
|
+
# @since 0.8.0
|
6
|
+
class Param < Base
|
7
|
+
class << self
|
8
|
+
# @param name [String, Symbol]
|
9
|
+
# @param type [String, Symbol, Any]
|
10
|
+
# @param type_system [String, Symbol]
|
11
|
+
# @param privacy [String, Symbol]
|
12
|
+
# @param finalize [String, Symbol, Proc]
|
13
|
+
# @param cast [Boolean]
|
14
|
+
# @param mutable [Boolean]
|
15
|
+
# @param as [String, Symbol, NilClass]
|
16
|
+
# @return [SmartCore::Initializer::Attribute::Value::Param]
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
# @since 0.8.0
|
20
|
+
def create(name, type, type_system, privacy, finalize, cast, mutable, as)
|
21
|
+
prepared_name = prepare_name_param(name)
|
22
|
+
prepared_privacy = prepare_privacy_param(privacy)
|
23
|
+
prepared_finalize = prepare_finalize_param(finalize)
|
24
|
+
prepared_cast = prepare_cast_param(cast)
|
25
|
+
prepared_type_system = prepare_type_system_param(type_system)
|
26
|
+
prepared_type = prepare_type_param(type, prepared_type_system)
|
27
|
+
prepared_mutable = prepare_mutable_param(mutable)
|
28
|
+
prepared_as = preapre_as_param(as)
|
29
|
+
|
30
|
+
create_attribute(
|
31
|
+
prepared_name,
|
32
|
+
prepared_type,
|
33
|
+
prepared_type_system,
|
34
|
+
prepared_privacy,
|
35
|
+
prepared_finalize,
|
36
|
+
prepared_cast,
|
37
|
+
prepared_mutable,
|
38
|
+
prepared_as
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# @param name [String]
|
45
|
+
# @param type [SmartCore::Initializer::TypeSystem::Interop]
|
46
|
+
# @param type_system [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
47
|
+
# @param privacy [Symbol]
|
48
|
+
# @param finalize [SmartCore::Initializer::Attribute::Finalizer::Abstract]
|
49
|
+
# @param cast [Boolean]
|
50
|
+
# @param mutable [Boolean]
|
51
|
+
# @param as [String, Symbol]
|
52
|
+
# @return [SmartCore::Initializer::Attribute::Value::Param]
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
# @since 0.8.0
|
56
|
+
def create_attribute(name, type, type_system, privacy, finalize, cast, mutable, as)
|
57
|
+
SmartCore::Initializer::Attribute::Value::Param.new(
|
58
|
+
name, type, type_system, privacy, finalize, cast, mutable, as
|
59
|
+
)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -2,203 +2,9 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
# @param type_system [String, Symbol]
|
11
|
-
# @param privacy [String, Symbol]
|
12
|
-
# @param finalize [String, Symbol, Proc]
|
13
|
-
# @param cast [Boolean]
|
14
|
-
# @param read_only [Boolean]
|
15
|
-
# @param as [String, Symbol, NilClass]
|
16
|
-
# @param dynamic_options [Hash<Symbol,Any>]
|
17
|
-
# @return [SmartCore::Initializer::Attribute]
|
18
|
-
#
|
19
|
-
# @api private
|
20
|
-
# @since 0.1.0
|
21
|
-
def create(name, type, type_system, privacy, finalize, cast, read_only, as, dynamic_options)
|
22
|
-
prepared_name = prepare_name_param(name)
|
23
|
-
prepared_privacy = prepare_privacy_param(privacy)
|
24
|
-
prepared_finalize = prepare_finalize_param(finalize)
|
25
|
-
prepared_cast = prepare_cast_param(cast)
|
26
|
-
prepared_type_system = prepare_type_system_param(type_system)
|
27
|
-
prepared_type = prepare_type_param(type, prepared_type_system)
|
28
|
-
prepared_read_only = prepare_read_only_param(read_only)
|
29
|
-
prepared_as = preapre_as_param(as)
|
30
|
-
prepared_dynamic_options = prepare_dynamic_options_param(dynamic_options)
|
31
|
-
|
32
|
-
create_attribute(
|
33
|
-
prepared_name,
|
34
|
-
prepared_type,
|
35
|
-
prepared_type_system,
|
36
|
-
prepared_privacy,
|
37
|
-
prepared_finalize,
|
38
|
-
prepared_cast,
|
39
|
-
prepared_read_only,
|
40
|
-
prepared_as,
|
41
|
-
prepared_dynamic_options
|
42
|
-
)
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
# @param name [String, Symbol]
|
48
|
-
# @return [Symbol]
|
49
|
-
#
|
50
|
-
# @api private
|
51
|
-
# @since 0.1.0
|
52
|
-
def prepare_name_param(name)
|
53
|
-
unless name.is_a?(String) || name.is_a?(Symbol)
|
54
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
55
|
-
Attribute name should be a type of String or Symbol
|
56
|
-
ERROR_MESSAGE
|
57
|
-
end
|
58
|
-
|
59
|
-
name.to_sym
|
60
|
-
end
|
61
|
-
|
62
|
-
# @param type [String, Symbol, Any]
|
63
|
-
# @param type_system [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
64
|
-
# @return [SmartCore::Initializer::TypeSystem::Interop]
|
65
|
-
#
|
66
|
-
# @api private
|
67
|
-
# @since 0.1.0
|
68
|
-
def prepare_type_param(type, type_system)
|
69
|
-
type_primitive =
|
70
|
-
if type.is_a?(String) || type.is_a?(Symbol)
|
71
|
-
type_system.type_from_alias(type)
|
72
|
-
else
|
73
|
-
type
|
74
|
-
end
|
75
|
-
|
76
|
-
type_system.create(type_primitive)
|
77
|
-
end
|
78
|
-
|
79
|
-
# @param cast [Boolean]
|
80
|
-
# @return [Boolean]
|
81
|
-
#
|
82
|
-
# @api private
|
83
|
-
# @since 0.1.0
|
84
|
-
def prepare_cast_param(cast)
|
85
|
-
unless cast.is_a?(TrueClass) || cast.is_a?(FalseClass)
|
86
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
87
|
-
Attribute cast should be a type of boolean
|
88
|
-
ERROR_MESSAGE
|
89
|
-
end
|
90
|
-
|
91
|
-
cast
|
92
|
-
end
|
93
|
-
|
94
|
-
# @param privacy [String, Symbol]
|
95
|
-
# @return [Symbol]
|
96
|
-
#
|
97
|
-
# @api private
|
98
|
-
# @since 0.1.0
|
99
|
-
def prepare_privacy_param(privacy)
|
100
|
-
unless privacy.is_a?(String) || privacy.is_a?(Symbol)
|
101
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
102
|
-
Attribute privacy should be a type of String or Symbol
|
103
|
-
ERROR_MESSAGE
|
104
|
-
end
|
105
|
-
|
106
|
-
SmartCore::Initializer::Attribute::Parameters::PRIVACY_MODES.fetch(privacy.to_sym) do
|
107
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
108
|
-
Incorrect attribute privacy identifier "#{privacy}"
|
109
|
-
ERROR_MESSAGE
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
# @param finalize [String, Symbol, Proc]
|
114
|
-
# @return [SmartCore::Initializer::Attribute::Finalizer::AnonymousBlock/InstanceMethod]
|
115
|
-
#
|
116
|
-
# @api private
|
117
|
-
# @since 0.1.0
|
118
|
-
def prepare_finalize_param(finalize)
|
119
|
-
unless finalize.is_a?(String) || finalize.is_a?(Symbol) || finalize.is_a?(Proc)
|
120
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
121
|
-
Attribute finalizer should be a type of String, Symbol or Proc
|
122
|
-
ERROR_MESSAGE
|
123
|
-
end
|
124
|
-
|
125
|
-
SmartCore::Initializer::Attribute::Finalizer.create(finalize)
|
126
|
-
end
|
127
|
-
|
128
|
-
# @param read_only [Boolean]
|
129
|
-
# @return [Boolean]
|
130
|
-
#
|
131
|
-
# @api private
|
132
|
-
# @since 0.4.0
|
133
|
-
def prepare_read_only_param(read_only)
|
134
|
-
unless read_only.is_a?(FalseClass) || read_only.is_a?(TrueClass)
|
135
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
136
|
-
:read_only attribute should be a type of boolean
|
137
|
-
ERROR_MESSAGE
|
138
|
-
end
|
139
|
-
|
140
|
-
read_only
|
141
|
-
end
|
142
|
-
|
143
|
-
# @param [String, Symbol, NilClass]
|
144
|
-
# @return [String, Symbol, NilClass]
|
145
|
-
#
|
146
|
-
# @api private
|
147
|
-
# @since 0.4.0
|
148
|
-
def preapre_as_param(as)
|
149
|
-
unless as.is_a?(NilClass) || as.is_a?(String) || as.is_a?(Symbol)
|
150
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
151
|
-
Attribute alias should be a type of String or Symbol
|
152
|
-
ERROR_MESSAGE
|
153
|
-
end
|
154
|
-
|
155
|
-
as
|
156
|
-
end
|
157
|
-
|
158
|
-
# @param dynamic_options [Hash<Symbol,Any>]
|
159
|
-
# @return [Hash<Symbol,Any>]
|
160
|
-
#
|
161
|
-
# @api private
|
162
|
-
# @since 0.1.0
|
163
|
-
def prepare_dynamic_options_param(dynamic_options)
|
164
|
-
# :nocov:
|
165
|
-
unless dynamic_options.is_a?(Hash)
|
166
|
-
raise(SmartCore::Initializer::ArgumentError, <<~ERROR_MESSAGE)
|
167
|
-
Attribute dynamic options should be a type of Hash
|
168
|
-
ERROR_MESSAGE
|
169
|
-
end
|
170
|
-
# :nocov:
|
171
|
-
|
172
|
-
dynamic_options
|
173
|
-
end
|
174
|
-
|
175
|
-
# @param type_system [String, Symbol]
|
176
|
-
# @return [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
177
|
-
#
|
178
|
-
# @api private
|
179
|
-
# @since 0.1.0
|
180
|
-
def prepare_type_system_param(type_system)
|
181
|
-
SmartCore::Initializer::TypeSystem.resolve(type_system)
|
182
|
-
end
|
183
|
-
|
184
|
-
# @param name [String]
|
185
|
-
# @param type [SmartCore::Initializer::TypeSystem::Interop]
|
186
|
-
# @param type_system [Class<SmartCore::Initializer::TypeSystem::Interop>]
|
187
|
-
# @param privacy [Symbol]
|
188
|
-
# @param finalize [SmartCore::Initializer::Attribute::Finalizer::AnonymousBlock/InstanceMethod]
|
189
|
-
# @param cast [Boolean]
|
190
|
-
# @param dynamic_options [Hash<Symbol,String>]
|
191
|
-
# @return [SmartCore::Initializer::Attribute]
|
192
|
-
#
|
193
|
-
# @api private
|
194
|
-
# @since 0.1.0
|
195
|
-
# rubocop:disable Layout/LineLength
|
196
|
-
def create_attribute(name, type, type_system, privacy, finalize, cast, read_only, as, dynamic_options)
|
197
|
-
SmartCore::Initializer::Attribute.new(
|
198
|
-
name, type, type_system, privacy, finalize, cast, read_only, as, dynamic_options
|
199
|
-
)
|
200
|
-
end
|
201
|
-
# rubocop:enable Layout/LineLength
|
202
|
-
end
|
5
|
+
# @version 0.8.0
|
6
|
+
module SmartCore::Initializer::Attribute::Factory
|
7
|
+
require_relative 'factory/base'
|
8
|
+
require_relative 'factory/option'
|
9
|
+
require_relative 'factory/param'
|
203
10
|
end
|
204
|
-
# rubocop:enable Metrics/ClassLength
|