smart_types 0.1.0 → 0.2.0
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/.rubocop.yml +6 -1
- data/CHANGELOG.md +17 -0
- data/Gemfile.lock +59 -48
- data/README.md +178 -31
- data/Rakefile +0 -1
- data/bin/console +2 -2
- data/lib/smart_core/types/primitive.rb +54 -15
- data/lib/smart_core/types/primitive/factory.rb +46 -7
- data/lib/smart_core/types/primitive/factory/definition_context.rb +114 -4
- data/lib/smart_core/types/primitive/invariant_control.rb +64 -0
- data/lib/smart_core/types/primitive/invariant_control/chain.rb +58 -0
- data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
- data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
- data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
- data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
- data/lib/smart_core/types/primitive/invariant_control/single.rb +54 -0
- data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
- data/lib/smart_core/types/primitive/mult_factory.rb +25 -10
- data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +4 -2
- data/lib/smart_core/types/primitive/mult_validator.rb +34 -0
- data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
- data/lib/smart_core/types/primitive/nilable_factory.rb +24 -9
- data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
- data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
- data/lib/smart_core/types/primitive/sum_factory.rb +25 -10
- data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +3 -1
- data/lib/smart_core/types/primitive/sum_validator.rb +101 -0
- data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
- data/lib/smart_core/types/primitive/validator.rb +84 -0
- data/lib/smart_core/types/primitive/validator/result.rb +78 -0
- data/lib/smart_core/types/value.rb +9 -0
- 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/io.rb +13 -0
- data/lib/smart_core/types/value/method.rb +9 -0
- data/lib/smart_core/types/value/nil.rb +0 -2
- 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 +13 -0
- data/lib/smart_core/types/value/string_io.rb +13 -0
- data/lib/smart_core/types/value/unbound_method.rb +9 -0
- data/lib/smart_core/types/version.rb +2 -1
- data/smart_types.gemspec +5 -4
- metadata +53 -17
- 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,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
class SmartCore::Types::Primitive::SumValidator::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 concrete_validation_result [
|
19
|
+
# SmartCore::Types::Primitive::Validator::Result,
|
20
|
+
# SmartCore::Types::Primitive::SumValidator::Result,
|
21
|
+
# SmartCore::Types::Primitive::MultValidator::Result,
|
22
|
+
# SmartCore::Types::Primitive::NilableValidator::Result
|
23
|
+
# ]
|
24
|
+
#
|
25
|
+
# @api private
|
26
|
+
# @since 0.2.0
|
27
|
+
attr_reader :concrete_validation_result
|
28
|
+
|
29
|
+
# @return [Array<String>]
|
30
|
+
#
|
31
|
+
# @api public
|
32
|
+
# @since 0.2.0
|
33
|
+
attr_reader :invariant_errors
|
34
|
+
alias_method :errors, :invariant_errors
|
35
|
+
alias_method :error_codes, :invariant_errors
|
36
|
+
|
37
|
+
# @param type [SmartCore::Types::Primitive]
|
38
|
+
# @param concrete_validation_result [
|
39
|
+
# Boolean, (TEMPORARY)
|
40
|
+
# SmartCore::Types::Primitive::Validator::Result,
|
41
|
+
# SmartCore::Types::Primitive::SumValidator::Result,
|
42
|
+
# SmartCore::Types::Primitive::MultValidator::Result,
|
43
|
+
# SmartCore::Types::Primitive::NilableValidator::Result
|
44
|
+
# ]
|
45
|
+
# @param invariant_errors [Array<String>]
|
46
|
+
# @return [void]
|
47
|
+
#
|
48
|
+
# @api private
|
49
|
+
# @since 0.2.0
|
50
|
+
def initialize(type, concrete_validation_result, invariant_errors = NO_INVARIANT_ERRORS.dup)
|
51
|
+
@type = type
|
52
|
+
@concrete_validation_result = concrete_validation_result
|
53
|
+
@invariant_errors = invariant_errors.dup.tap(&:freeze)
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [Boolean]
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
# @since 0.2.0
|
60
|
+
def valid_invariants?
|
61
|
+
# NOTE: at this moment type sum does not support invariant checking
|
62
|
+
# TODO (0.x.0):
|
63
|
+
# concrete_validation_result.valid_invariants?
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
# @return [Boolean]
|
68
|
+
#
|
69
|
+
# @api public
|
70
|
+
# @since 0.2.0
|
71
|
+
def is_valid_check
|
72
|
+
# NOTE: at this moment type sum does not support invariant checking
|
73
|
+
# TODO (0.x.0):
|
74
|
+
# concrete_validation_result.is_valid_check
|
75
|
+
concrete_validation_result
|
76
|
+
end
|
77
|
+
alias_method :valid_check?, :is_valid_check
|
78
|
+
|
79
|
+
# @return [Boolean]
|
80
|
+
#
|
81
|
+
# @api public
|
82
|
+
# @since 0.2.0
|
83
|
+
def success?
|
84
|
+
# NOTE: at this moment type sum does not support invariant checking
|
85
|
+
# TODO (0.x.0):
|
86
|
+
# concrete_validation_result.success?
|
87
|
+
concrete_validation_result
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Boolean]
|
91
|
+
#
|
92
|
+
# @api public
|
93
|
+
# @since 0.2.0
|
94
|
+
def failure?
|
95
|
+
# NOTE: at this moment type sum does not support invariant checking
|
96
|
+
# TODO (0.x.0):
|
97
|
+
# concrete_validation_result.failure?
|
98
|
+
!success?
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
class SmartCore::Types::Primitive::Validator
|
6
|
+
require_relative 'validator/result'
|
7
|
+
|
8
|
+
# @return [SmartCore::Type::Primitive]
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @since 0.2.0
|
12
|
+
attr_reader :type
|
13
|
+
|
14
|
+
# @return [SmartCore::Types::Primitive::Checker]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
# @since 0.2.0
|
18
|
+
attr_reader :type_checker
|
19
|
+
|
20
|
+
# @return [SmartCore::Types::Primitive::InvariantControl]
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
# @since 0.2.0
|
24
|
+
attr_reader :invariant_control
|
25
|
+
|
26
|
+
# @param type_checker [
|
27
|
+
# SmartCore::Types::Primitive::Checker,
|
28
|
+
# SmartCore::Types::Primitive::MultChecker,
|
29
|
+
# SmartCore::Types::Primitive::SumChecker
|
30
|
+
# ]
|
31
|
+
# @param invariant_control [SmartCore::Types::Primitive::InvariantControl]
|
32
|
+
# @return [void]
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
# @since 0.2.0
|
36
|
+
def initialize(type_checker, invariant_control)
|
37
|
+
@type = nil
|
38
|
+
@type_checker = type_checker
|
39
|
+
@invariant_control = invariant_control
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param type [SmartCore::Types::Primitive]
|
43
|
+
# @return [void]
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
# @since 0.2.0
|
47
|
+
def ___assign_type___(type)
|
48
|
+
@type = type
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param value [Any]
|
52
|
+
# @return [Boolean]
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
# @since 0.2.0
|
56
|
+
def valid?(value)
|
57
|
+
validate(value).success?
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param value [Any]
|
61
|
+
# @return [SmartCore::Types::Primitive::Validator::Result]
|
62
|
+
#
|
63
|
+
# @api private
|
64
|
+
# @since 0.2.0
|
65
|
+
def validate(value)
|
66
|
+
checker_result = type_checker.call(value) # => Boolean
|
67
|
+
return Result.new(type, value, checker_result) unless checker_result
|
68
|
+
invariant_result = invariant_control.check(value)
|
69
|
+
invariant_errors = invariant_result.invariant_errors.map { |error| "#{type.name}.#{error}" }
|
70
|
+
Result.new(type, value, checker_result, invariant_errors)
|
71
|
+
end
|
72
|
+
|
73
|
+
# @param value [Any]
|
74
|
+
# @return [void]
|
75
|
+
#
|
76
|
+
# @raise [SmartCore::Types::TypeError]
|
77
|
+
#
|
78
|
+
# @api private
|
79
|
+
# @since 0.2.0
|
80
|
+
def validate!(value)
|
81
|
+
return if validate(value).success?
|
82
|
+
raise(SmartCore::Types::TypeError, 'Invalid type')
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.2.0
|
5
|
+
class SmartCore::Types::Primitive::Validator::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 [Boolean]
|
19
|
+
#
|
20
|
+
# @api public
|
21
|
+
# @since 0.2.0
|
22
|
+
attr_reader :is_valid_check
|
23
|
+
alias_method :valid_check?, :is_valid_check
|
24
|
+
|
25
|
+
# @return [Any]
|
26
|
+
#
|
27
|
+
# @api public
|
28
|
+
# @since 0.2.0
|
29
|
+
attr_reader :checked_value
|
30
|
+
alias_method :value, :checked_value
|
31
|
+
|
32
|
+
# @return [Array<String>]
|
33
|
+
#
|
34
|
+
# @api public
|
35
|
+
# @since 0.2.0
|
36
|
+
attr_reader :invariant_errors
|
37
|
+
alias_method :errors, :invariant_errors
|
38
|
+
alias_method :error_codes, :invariant_errors
|
39
|
+
|
40
|
+
# @param type [SmartCore::Types::Primitive]
|
41
|
+
# @param checked_value [Any]
|
42
|
+
# @param is_valid_check [Boolean]
|
43
|
+
# @param invariant_errors [Array<String>]
|
44
|
+
# @return [void]
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
# @since 0.2.0
|
48
|
+
def initialize(type, checked_value, is_valid_check, invariant_errors = NO_INVARIANT_ERRORS.dup)
|
49
|
+
@type = type
|
50
|
+
@checked_value = checked_value
|
51
|
+
@is_valid_check = is_valid_check
|
52
|
+
@invariant_errors = invariant_errors.tap(&:freeze)
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [Boolean]
|
56
|
+
#
|
57
|
+
# @api public
|
58
|
+
# @since 0.2.0
|
59
|
+
def valid_invariants?
|
60
|
+
invariant_errors.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Boolean]
|
64
|
+
#
|
65
|
+
# @api public
|
66
|
+
# @since 0.2.0
|
67
|
+
def success?
|
68
|
+
valid_check? && invariant_errors.empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [Boolean]
|
72
|
+
#
|
73
|
+
# @api public
|
74
|
+
# @since 0.2.0
|
75
|
+
def failure?
|
76
|
+
!success?
|
77
|
+
end
|
78
|
+
end
|
@@ -5,6 +5,7 @@
|
|
5
5
|
class SmartCore::Types::Value < SmartCore::Types::Primitive
|
6
6
|
require_relative 'value/nil'
|
7
7
|
require_relative 'value/string'
|
8
|
+
require_relative 'value/string_io'
|
8
9
|
require_relative 'value/symbol'
|
9
10
|
require_relative 'value/text'
|
10
11
|
require_relative 'value/integer'
|
@@ -13,6 +14,7 @@ class SmartCore::Types::Value < SmartCore::Types::Primitive
|
|
13
14
|
require_relative 'value/boolean'
|
14
15
|
require_relative 'value/array'
|
15
16
|
require_relative 'value/hash'
|
17
|
+
require_relative 'value/io'
|
16
18
|
require_relative 'value/proc'
|
17
19
|
require_relative 'value/class'
|
18
20
|
require_relative 'value/module'
|
@@ -22,6 +24,13 @@ class SmartCore::Types::Value < SmartCore::Types::Primitive
|
|
22
24
|
require_relative 'value/date_time'
|
23
25
|
require_relative 'value/time_based'
|
24
26
|
require_relative 'value/enumerable'
|
27
|
+
require_relative 'value/enumerator'
|
28
|
+
require_relative 'value/enumerator_chain'
|
25
29
|
require_relative 'value/comparable'
|
26
30
|
require_relative 'value/big_decimal'
|
31
|
+
require_relative 'value/range'
|
32
|
+
require_relative 'value/method'
|
33
|
+
require_relative 'value/unbound_method'
|
34
|
+
require_relative 'value/set'
|
35
|
+
require_relative 'value/rational'
|
27
36
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api public
|
4
|
+
# @since 0.x.0
|
5
|
+
SmartCore::Types::Value.define_type(:EnumeratorChain) do |type|
|
6
|
+
type.define_checker do |value|
|
7
|
+
# TODO: realize
|
8
|
+
end
|
9
|
+
|
10
|
+
type.define_caster do |value|
|
11
|
+
# TODO: realize
|
12
|
+
end
|
13
|
+
end
|
@@ -4,8 +4,6 @@
|
|
4
4
|
# @since 0.1.0
|
5
5
|
SmartCore::Types::Value.define_type(:Nil) do |type|
|
6
6
|
type.define_checker do |value|
|
7
|
-
# rubocop:disable Style/NilComparison
|
8
7
|
value == nil # NOTE: #nil? is not used cuz BasicObject hasn't #nil? method
|
9
|
-
# rubocop:enable Style/NilComparison
|
10
8
|
end
|
11
9
|
end
|
data/smart_types.gemspec
CHANGED
@@ -32,11 +32,12 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
33
|
spec.require_paths = ['lib']
|
34
34
|
|
35
|
-
spec.add_dependency 'smart_engine', '~> 0.
|
35
|
+
spec.add_dependency 'smart_engine', '~> 0.7'
|
36
36
|
|
37
37
|
spec.add_development_dependency 'bundler', '~> 2.1'
|
38
38
|
spec.add_development_dependency 'rake', '~> 13.0'
|
39
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
40
|
-
spec.add_development_dependency 'armitage-rubocop', '~>
|
41
|
-
spec.add_development_dependency 'simplecov', '~> 0.
|
39
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
40
|
+
spec.add_development_dependency 'armitage-rubocop', '~> 1.3'
|
41
|
+
spec.add_development_dependency 'simplecov', '~> 0.19'
|
42
|
+
spec.add_development_dependency 'pry', '~> 0.13'
|
42
43
|
end
|