smart_types 0.1.0 → 0.6.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 +6 -1
- data/CHANGELOG.md +57 -0
- data/Gemfile.lock +71 -58
- data/LICENSE.txt +1 -1
- data/README.md +363 -34
- data/Rakefile +0 -1
- data/bin/console +2 -2
- data/lib/smart_core/types/errors.rb +12 -0
- data/lib/smart_core/types/primitive/caster.rb +5 -2
- data/lib/smart_core/types/primitive/checker.rb +5 -2
- data/lib/smart_core/types/primitive/factory/definition_context.rb +140 -4
- data/lib/smart_core/types/primitive/factory/runtime_type_builder.rb +53 -0
- data/lib/smart_core/types/primitive/factory.rb +104 -7
- data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
- data/lib/smart_core/types/primitive/invariant_control/chain.rb +61 -0
- data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
- data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
- data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
- data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
- data/lib/smart_core/types/primitive/invariant_control/single.rb +57 -0
- data/lib/smart_core/types/primitive/invariant_control.rb +67 -0
- data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +26 -2
- data/lib/smart_core/types/primitive/mult_factory.rb +59 -10
- data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
- data/lib/smart_core/types/primitive/mult_validator.rb +42 -0
- data/lib/smart_core/types/primitive/nilable_factory.rb +31 -9
- data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
- data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
- data/lib/smart_core/types/primitive/runtime_attributes_checker.rb +77 -0
- data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +25 -1
- data/lib/smart_core/types/primitive/sum_factory.rb +59 -10
- data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
- data/lib/smart_core/types/primitive/sum_validator.rb +117 -0
- data/lib/smart_core/types/primitive/undefined_caster.rb +4 -1
- data/lib/smart_core/types/primitive/validator/result.rb +78 -0
- data/lib/smart_core/types/primitive/validator.rb +93 -0
- data/lib/smart_core/types/primitive.rb +99 -15
- data/lib/smart_core/types/protocol/instance_of.rb +19 -0
- data/lib/smart_core/types/protocol.rb +7 -0
- data/lib/smart_core/types/value/array.rb +3 -0
- data/lib/smart_core/types/value/big_decimal.rb +4 -1
- data/lib/smart_core/types/value/boolean.rb +3 -0
- data/lib/smart_core/types/value/class.rb +3 -0
- data/lib/smart_core/types/value/date.rb +3 -0
- data/lib/smart_core/types/value/date_time.rb +3 -0
- data/lib/smart_core/types/value/enumerable.rb +1 -1
- data/lib/smart_core/types/value/enumerator.rb +13 -0
- data/lib/smart_core/types/value/enumerator_chain.rb +13 -0
- data/lib/smart_core/types/value/float.rb +3 -0
- data/lib/smart_core/types/value/hash.rb +3 -0
- data/lib/smart_core/types/value/integer.rb +3 -0
- data/lib/smart_core/types/value/io.rb +13 -0
- data/lib/smart_core/types/value/method.rb +9 -0
- data/lib/smart_core/types/value/module.rb +3 -0
- data/lib/smart_core/types/value/nil.rb +0 -2
- data/lib/smart_core/types/value/numeric.rb +3 -0
- data/lib/smart_core/types/value/proc.rb +3 -0
- data/lib/smart_core/types/value/range.rb +9 -0
- data/lib/smart_core/types/value/rational.rb +13 -0
- data/lib/smart_core/types/value/set.rb +21 -0
- data/lib/smart_core/types/value/string.rb +3 -0
- data/lib/smart_core/types/value/string_io.rb +15 -0
- data/lib/smart_core/types/value/symbol.rb +3 -0
- data/lib/smart_core/types/value/text.rb +6 -4
- data/lib/smart_core/types/value/time.rb +3 -0
- data/lib/smart_core/types/value/time_based.rb +8 -5
- data/lib/smart_core/types/value/unbound_method.rb +9 -0
- data/lib/smart_core/types/value.rb +9 -0
- data/lib/smart_core/types/variadic/array_of.rb +23 -0
- data/lib/smart_core/types/variadic/enum.rb +11 -0
- data/lib/smart_core/types/variadic/tuple.rb +23 -0
- data/lib/smart_core/types/variadic.rb +10 -0
- data/lib/smart_core/types/version.rb +2 -1
- data/lib/smart_core/types.rb +2 -0
- data/smart_types.gemspec +5 -4
- metadata +61 -18
- data/.travis.yml +0 -21
- data/lib/smart_core/types/primitive/mult_checker.rb +0 -31
- data/lib/smart_core/types/primitive/nilable_checker.rb +0 -37
- data/lib/smart_core/types/primitive/sum_checker.rb +0 -31
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
# @version 0.3.0
|
6
|
+
class SmartCore::Types::Primitive::InvariantControl
|
7
|
+
require_relative 'invariant_control/result'
|
8
|
+
require_relative 'invariant_control/single'
|
9
|
+
require_relative 'invariant_control/chain'
|
10
|
+
require_relative 'invariant_control/factory'
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# @param invariant_chains [Hash<String,Array<Proc>>]
|
14
|
+
# @param invariants [Hash<String,Proc>]
|
15
|
+
# @return [SmartCore::Types::Primitive::InvariantControl]
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
# @since 0.2.0
|
19
|
+
def create(invariant_chains, invariants)
|
20
|
+
Factory.create(invariant_chains, invariants)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param invariant_chains [Array<SmartCore::Types::Primitive::InvariantControl::Chain>]
|
25
|
+
# @param invariants [Array<SmartCore::Types::Primitive::InvariantControl::Single>]
|
26
|
+
# @return [void]
|
27
|
+
#
|
28
|
+
# @api private
|
29
|
+
# @since 0.2.0
|
30
|
+
def initialize(invariant_chains, invariants)
|
31
|
+
@invariant_chains = invariant_chains
|
32
|
+
@invariants = invariants
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param value [Any]
|
36
|
+
# @param runtime_attributes [Array<Any>]
|
37
|
+
# @return [SmartCore::Types::Primitive::InvariantControl::Result]
|
38
|
+
#
|
39
|
+
# @api private
|
40
|
+
# @since 0.2.0
|
41
|
+
# @version 0.3.0
|
42
|
+
def check(value, runtime_attributes)
|
43
|
+
Result.new(self, value).tap do |result|
|
44
|
+
invariant_chains.each do |chain|
|
45
|
+
result.add_chain_result(chain.check(value, runtime_attributes))
|
46
|
+
end
|
47
|
+
|
48
|
+
invariants.each do |invariant|
|
49
|
+
result.add_single_result(invariant.check(value, runtime_attributes))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# @return [Array<SmartCore::Types::Primitive::InvariantControl::Chain>]
|
57
|
+
#
|
58
|
+
# @api private
|
59
|
+
# @since 0.2.0
|
60
|
+
attr_reader :invariant_chains
|
61
|
+
|
62
|
+
# @return [Array<SmartCore::Types::Primitive::InvariantControl::Single>]
|
63
|
+
#
|
64
|
+
# @api private
|
65
|
+
# @since 0.2.0
|
66
|
+
attr_reader :invariants
|
67
|
+
end
|
@@ -2,19 +2,28 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
-
|
5
|
+
# @version 0.3.0
|
6
|
+
class SmartCore::Types::Primitive::MultFactory::DefinitionContext
|
6
7
|
# @return [Proc, NilClass]
|
7
8
|
#
|
8
9
|
# @api private
|
9
10
|
# @since 0.1.0
|
10
11
|
attr_reader :type_caster
|
11
12
|
|
13
|
+
# @return [Proc, NilClass]
|
14
|
+
#
|
15
|
+
# @api private
|
16
|
+
# @since 0.3.0
|
17
|
+
attr_reader :type_runtime_attributes_checker
|
18
|
+
|
12
19
|
# @return [void]
|
13
20
|
#
|
14
21
|
# @api private
|
15
22
|
# @since 0.1.0
|
23
|
+
# @version 0.3.0
|
16
24
|
def initialize
|
17
25
|
@type_caster = nil
|
26
|
+
@type_runtime_attributes_checker = nil
|
18
27
|
end
|
19
28
|
|
20
29
|
# @param caster [Block]
|
@@ -22,8 +31,23 @@ class SmartCore::Types::Primitive::Factory::DefinitionContext
|
|
22
31
|
#
|
23
32
|
# @api public
|
24
33
|
# @since 0.1.0
|
34
|
+
# @version 0.3.0
|
25
35
|
def define_caster(&caster)
|
26
|
-
raise(SmartCore::
|
36
|
+
raise(SmartCore::Types::TypeDefinitionError, 'No caster definition block') unless block_given?
|
27
37
|
@type_caster = caster
|
28
38
|
end
|
39
|
+
|
40
|
+
# @param definition [Block]
|
41
|
+
# @return [void]
|
42
|
+
#
|
43
|
+
# @api public
|
44
|
+
# @since 0.3.0
|
45
|
+
def runtime_attributes_checker(&definition)
|
46
|
+
unless block_given?
|
47
|
+
raise(SmartCore::Types::TypeDefinitionError, 'No runtime checker definition block')
|
48
|
+
end
|
49
|
+
@type_runtime_attributes_checker = definition
|
50
|
+
end
|
51
|
+
|
52
|
+
# TODO (0.x.0): invariant API
|
29
53
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
+
# @version 0.3.0
|
5
6
|
module SmartCore::Types::Primitive::MultFactory
|
6
7
|
require_relative 'mult_factory/definition_context'
|
7
8
|
|
@@ -12,11 +13,20 @@ module SmartCore::Types::Primitive::MultFactory
|
|
12
13
|
#
|
13
14
|
# @api private
|
14
15
|
# @since 0.1.0
|
16
|
+
# @version 0.3.0
|
15
17
|
def create_type(types, type_definition)
|
16
18
|
type_definitions = build_type_definitions(type_definition)
|
17
|
-
|
19
|
+
type_runtime_attributes_checker = build_type_runtime_attributes_checker(type_definitions)
|
20
|
+
type_validator = build_type_validator(types, type_definitions)
|
18
21
|
type_caster = build_type_caster(types, type_definitions)
|
19
|
-
build_type(
|
22
|
+
build_type(
|
23
|
+
type_validator,
|
24
|
+
type_caster,
|
25
|
+
type_runtime_attributes_checker
|
26
|
+
).tap do |type|
|
27
|
+
assign_type_validator(type, type_validator)
|
28
|
+
assign_type_runtime_attributes_checker(type, type_runtime_attributes_checker)
|
29
|
+
end
|
20
30
|
end
|
21
31
|
|
22
32
|
private
|
@@ -32,14 +42,25 @@ module SmartCore::Types::Primitive::MultFactory
|
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
45
|
+
# @param type_definitions [SmartCore::Types::Primitive::MultFactory::DefinitionContext]
|
46
|
+
# @return [SmartCore::Types::Primitive::RuntimeAttributesChecker]
|
47
|
+
#
|
48
|
+
# @api private
|
49
|
+
# @since 0.3.0
|
50
|
+
def build_type_runtime_attributes_checker(type_definitions)
|
51
|
+
SmartCore::Types::Primitive::RuntimeAttributesChecker.new(
|
52
|
+
type_definitions.type_runtime_attributes_checker
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
35
56
|
# @param types [Array<SmartCore::Types::Primtive>]
|
36
57
|
# @param type_definitions [SmartCore::Types::Primitive::MultFactory::DefinitionContext]
|
37
|
-
# @return [SmartCore::Types::Primitive::
|
58
|
+
# @return [SmartCore::Types::Primitive::MultValidator]
|
38
59
|
#
|
39
60
|
# @api private
|
40
|
-
# @since 0.
|
41
|
-
def
|
42
|
-
SmartCore::Types::Primitive::
|
61
|
+
# @since 0.2.0
|
62
|
+
def build_type_validator(types, type_definitions)
|
63
|
+
SmartCore::Types::Primitive::MultValidator.new(*types.map(&:validator))
|
43
64
|
end
|
44
65
|
|
45
66
|
# @param types [Array<SmartCore::Types::Primtive>]
|
@@ -49,21 +70,49 @@ module SmartCore::Types::Primitive::MultFactory
|
|
49
70
|
# @api private
|
50
71
|
# @since 0.1.0
|
51
72
|
def build_type_caster(types, type_definitions)
|
52
|
-
if type_definitions.type_caster
|
73
|
+
if type_definitions.type_caster == nil
|
53
74
|
SmartCore::Types::Primitive::UndefinedCaster.new
|
54
75
|
else
|
55
76
|
SmartCore::Types::Primitive::Caster.new(type_definitions.type_caster)
|
56
77
|
end
|
57
78
|
end
|
58
79
|
|
59
|
-
# @param
|
80
|
+
# @param type [SmartCore::Types::Primitive]
|
81
|
+
# @param type_validator [SmartCore::Types::Primitive::MultValidator]
|
82
|
+
# @return [void]
|
83
|
+
#
|
84
|
+
# @api private
|
85
|
+
# @since 0.2.0
|
86
|
+
def assign_type_validator(type, type_validator)
|
87
|
+
type_validator.___assign_type___(type)
|
88
|
+
end
|
89
|
+
|
90
|
+
# @param type [SmartCore::Types::Primitive]
|
91
|
+
# @param type_runtime_attributes_checker [SmartCore::Types::Primitive::RuntimeAttributesChecker]
|
92
|
+
# @return [void]
|
93
|
+
#
|
94
|
+
# @api private
|
95
|
+
# @since 0.3.0
|
96
|
+
def assign_type_runtime_attributes_checker(type, type_runtime_attributes_checker)
|
97
|
+
type_runtime_attributes_checker.___assign_type___(type)
|
98
|
+
end
|
99
|
+
|
100
|
+
# @param type_validator [SmartCore::Types::Primitive::MultValidator]
|
60
101
|
# @param type_caster [SmartCore::Types::Primitive::Caster]
|
102
|
+
# @param type_runtime_attributes_checker [SmartCore::Types::Primitive::RuntimeAttributesChecker]
|
61
103
|
# @return [SmartCore::Types::Primitive]
|
62
104
|
#
|
63
105
|
# @api private
|
64
106
|
# @since 0.1.0
|
65
|
-
|
66
|
-
|
107
|
+
# @version 0.3.0
|
108
|
+
def build_type(type_validator, type_caster, type_runtime_attributes_checker)
|
109
|
+
SmartCore::Types::Primitive.new(
|
110
|
+
nil,
|
111
|
+
nil,
|
112
|
+
type_validator,
|
113
|
+
type_caster,
|
114
|
+
type_runtime_attributes_checker
|
115
|
+
)
|
67
116
|
end
|
68
117
|
end
|
69
118
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
# @version 0.3.0
|
6
|
+
class SmartCore::Types::Primitive::MultValidator < SmartCore::Types::Primitive::SumValidator
|
7
|
+
require_relative 'mult_validator/result'
|
8
|
+
|
9
|
+
# @overload validate(value)
|
10
|
+
# @param value [Any]
|
11
|
+
# @return [SmartCore::Types::Primitive::MultValidator::Result]
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
# @since 0.2.0
|
15
|
+
|
16
|
+
# @overload ___copy_for___(type)
|
17
|
+
# @param type [SmartCore::Types::Primitive]
|
18
|
+
# @return [SmartCore::Types::Primitive::MultValidator]
|
19
|
+
#
|
20
|
+
# @api private
|
21
|
+
# @since 0.3.0
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# @param validation [Block]
|
26
|
+
# @yieldparam [void]
|
27
|
+
# @yieldreturn [SmartCore::Engine::Atom]
|
28
|
+
# @return [SmartCore::Types::Primitive::MultValidator::Result]
|
29
|
+
#
|
30
|
+
# @api private
|
31
|
+
# @since 0.2.0
|
32
|
+
def compile_validation_result(&validation)
|
33
|
+
# NOTE: at this moment type sum does not support invariant checking
|
34
|
+
# TODO (0.x.0):
|
35
|
+
# @yieldreturn [SmartCore::Types::Primitive::Validator::Result]
|
36
|
+
# => and:
|
37
|
+
# SmartCore::Types::Primitive::MultValidator::Result.new(
|
38
|
+
# type, final_result.value, final_result.value.invariant_errors
|
39
|
+
# )
|
40
|
+
SmartCore::Types::Primitive::MultValidator::Result.new(type, yield.value)
|
41
|
+
end
|
42
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
+
# @version 0.3.0
|
5
6
|
module SmartCore::Types::Primitive::NilableFactory
|
6
7
|
class << self
|
7
8
|
# @param type [SmartCore::Types::Primitive]
|
@@ -9,21 +10,24 @@ module SmartCore::Types::Primitive::NilableFactory
|
|
9
10
|
#
|
10
11
|
# @api private
|
11
12
|
# @since 0.1.0
|
13
|
+
# @version 0.2.0
|
12
14
|
def create_type(type)
|
13
|
-
|
15
|
+
type_validator = build_type_validator(type)
|
14
16
|
type_caster = build_type_caster(type)
|
15
|
-
build_type(type,
|
17
|
+
build_type(type, type_validator, type_caster).tap do |new_type|
|
18
|
+
assign_type_validator(new_type, type_validator)
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
private
|
19
23
|
|
20
24
|
# @param type [SmartCore::Types::Primitive]
|
21
|
-
# @return [SmartCore::Types::Primitive::
|
25
|
+
# @return [SmartCore::Types::Primitive::NilableValidator]
|
22
26
|
#
|
23
27
|
# @api private
|
24
|
-
# @since 0.
|
25
|
-
def
|
26
|
-
SmartCore::Types::Primitive::
|
28
|
+
# @since 0.2.0
|
29
|
+
def build_type_validator(type)
|
30
|
+
SmartCore::Types::Primitive::NilableValidator.new(type.validator)
|
27
31
|
end
|
28
32
|
|
29
33
|
# @param type [SmartCore::Types::Primitive]
|
@@ -36,14 +40,32 @@ module SmartCore::Types::Primitive::NilableFactory
|
|
36
40
|
end
|
37
41
|
|
38
42
|
# @param type [SmartCore::Types::Primitive]
|
39
|
-
# @param
|
43
|
+
# @param type_validator [SmartCore::Types::Primitive::NilableValidator]
|
44
|
+
# @return [void]
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
# @since 0.2.0
|
48
|
+
def assign_type_validator(type, type_validator)
|
49
|
+
type_validator.___assign_type___(type)
|
50
|
+
end
|
51
|
+
|
52
|
+
# @param type [SmartCore::Types::Primitive]
|
53
|
+
# @param type_validator [SmartCore::Types::Primitive::NilableValidator]
|
40
54
|
# @param type_caster [SmartCore::Types::Caster]
|
41
55
|
# @return [SmartCore::Type::Primitive]
|
42
56
|
#
|
43
57
|
# @api private
|
44
58
|
# @since 0.1.0
|
45
|
-
|
46
|
-
|
59
|
+
# @version 0.3.0
|
60
|
+
def build_type(type, type_validator, type_caster)
|
61
|
+
Class.new(type.class).new(
|
62
|
+
type.name,
|
63
|
+
type.category,
|
64
|
+
type_validator,
|
65
|
+
type_caster,
|
66
|
+
type.runtime_attributes_checker,
|
67
|
+
*type.runtime_attributes
|
68
|
+
)
|
47
69
|
end
|
48
70
|
end
|
49
71
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
class SmartCore::Types::Primitive::NilableValidator::Result
|
6
|
+
# @return [Array]
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
# @since 0.2.0
|
10
|
+
NO_INVARIANT_ERRORS = [].freeze
|
11
|
+
|
12
|
+
# @return [SmartCore::Types::Primitive]
|
13
|
+
#
|
14
|
+
# @api public
|
15
|
+
# @since 0.2.0
|
16
|
+
attr_reader :type
|
17
|
+
|
18
|
+
# @return [Any]
|
19
|
+
#
|
20
|
+
# @api public
|
21
|
+
# @since 0.2.0
|
22
|
+
attr_reader :checked_value
|
23
|
+
alias_method :value, :checked_value
|
24
|
+
|
25
|
+
# @param type [SmartCore::Types::Primitive]
|
26
|
+
# @param checked_value [Any]
|
27
|
+
# @return [void]
|
28
|
+
#
|
29
|
+
# @api private
|
30
|
+
# @since 0.2.0
|
31
|
+
def initialize(type, checked_value)
|
32
|
+
@type = type
|
33
|
+
@checked_value = checked_value
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Boolean]
|
37
|
+
#
|
38
|
+
# @api public
|
39
|
+
# @since 0.2.0
|
40
|
+
def is_valid_check
|
41
|
+
true
|
42
|
+
end
|
43
|
+
alias_method :valid_check?, :is_valid_check
|
44
|
+
|
45
|
+
# @return [Array]
|
46
|
+
#
|
47
|
+
# @api public
|
48
|
+
# @since 0.2.0
|
49
|
+
def invariant_errors
|
50
|
+
NO_INVARIANT_ERRORS
|
51
|
+
end
|
52
|
+
alias_method :errors, :invariant_errors
|
53
|
+
alias_method :error_codes, :invariant_errors
|
54
|
+
|
55
|
+
# @return [Boolean]
|
56
|
+
#
|
57
|
+
# @api public
|
58
|
+
# @since 0.2.0
|
59
|
+
def valid_invariants?
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Boolean]
|
64
|
+
#
|
65
|
+
# @api public
|
66
|
+
# @since 0.2.0
|
67
|
+
def success?
|
68
|
+
true
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [Boolean]
|
72
|
+
#
|
73
|
+
# @api public
|
74
|
+
# @since 0.2.0
|
75
|
+
def failure?
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
class SmartCore::Types::Primitive::NilableValidator
|
6
|
+
require_relative 'nilable_validator/result'
|
7
|
+
|
8
|
+
# @param validator [
|
9
|
+
# SmartCore::Types::Primitive::Validator,
|
10
|
+
# SmartCore::Types::Primitive::SumValidator,
|
11
|
+
# SmartCore::Types::Primitive::MultValidator,
|
12
|
+
# SmartCore::Types::Primitive::NilableValidator
|
13
|
+
# ]
|
14
|
+
# @return [void]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
# @since 0.2.0
|
18
|
+
def initialize(validator)
|
19
|
+
@type = nil
|
20
|
+
@validator = validator
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param type [SmartCore::Types::Primitive]
|
24
|
+
# @return [void]
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
# @since 0.2.0
|
28
|
+
def ___assign_type___(type)
|
29
|
+
@type = type
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Boolean]
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
# @since 0.2.0
|
36
|
+
def valid?(value)
|
37
|
+
value == nil ? true : validator.valid?(value)
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param value [Any]
|
41
|
+
# @return [SmartCore::Types::Primitive::Validator::Result]
|
42
|
+
# @return [SmartCore::Types::Primitive::SumValidator::Result]
|
43
|
+
# @return [SmartCore::Types::Primitive::MultValidator::Result]
|
44
|
+
# @return [SmartCore::Types::Primitive::NilableValidator::Result]
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
# @since 0.2.0
|
48
|
+
def validate(value)
|
49
|
+
return Result.new(type, value) if value == nil
|
50
|
+
validator.validate(value)
|
51
|
+
end
|
52
|
+
|
53
|
+
# @param value [Any]
|
54
|
+
# @return [void]
|
55
|
+
#
|
56
|
+
# @raise [SmartCore::Types::TypeError]
|
57
|
+
#
|
58
|
+
# @api private
|
59
|
+
# @since 0.2.0
|
60
|
+
def validate!(value)
|
61
|
+
return if valid?(value)
|
62
|
+
raise(SmartCore::Types::TypeError, 'Invalid type')
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# @return [SmartCore::Types::Primitive]
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
# @since 0.2.0
|
71
|
+
attr_reader :type
|
72
|
+
|
73
|
+
# @return [
|
74
|
+
# SmartCore::Types::Primitive::Validator,
|
75
|
+
# SmartCore::Types::Primitive::SumValidator,
|
76
|
+
# SmartCore::Types::Primitive::MultValidator,
|
77
|
+
# SmartCore::Types::Primitive::NilableValidator
|
78
|
+
# ]
|
79
|
+
#
|
80
|
+
# @api private
|
81
|
+
# @since 0.2.0
|
82
|
+
attr_reader :validator
|
83
|
+
end
|