smart_types 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -1
  3. data/CHANGELOG.md +17 -0
  4. data/Gemfile.lock +59 -48
  5. data/README.md +178 -31
  6. data/Rakefile +0 -1
  7. data/bin/console +2 -2
  8. data/lib/smart_core/types/primitive.rb +54 -15
  9. data/lib/smart_core/types/primitive/factory.rb +46 -7
  10. data/lib/smart_core/types/primitive/factory/definition_context.rb +114 -4
  11. data/lib/smart_core/types/primitive/invariant_control.rb +64 -0
  12. data/lib/smart_core/types/primitive/invariant_control/chain.rb +58 -0
  13. data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
  14. data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
  15. data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
  16. data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
  17. data/lib/smart_core/types/primitive/invariant_control/single.rb +54 -0
  18. data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
  19. data/lib/smart_core/types/primitive/mult_factory.rb +25 -10
  20. data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +4 -2
  21. data/lib/smart_core/types/primitive/mult_validator.rb +34 -0
  22. data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
  23. data/lib/smart_core/types/primitive/nilable_factory.rb +24 -9
  24. data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
  25. data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
  26. data/lib/smart_core/types/primitive/sum_factory.rb +25 -10
  27. data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +3 -1
  28. data/lib/smart_core/types/primitive/sum_validator.rb +101 -0
  29. data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
  30. data/lib/smart_core/types/primitive/validator.rb +84 -0
  31. data/lib/smart_core/types/primitive/validator/result.rb +78 -0
  32. data/lib/smart_core/types/value.rb +9 -0
  33. data/lib/smart_core/types/value/enumerator.rb +13 -0
  34. data/lib/smart_core/types/value/enumerator_chain.rb +13 -0
  35. data/lib/smart_core/types/value/io.rb +13 -0
  36. data/lib/smart_core/types/value/method.rb +9 -0
  37. data/lib/smart_core/types/value/nil.rb +0 -2
  38. data/lib/smart_core/types/value/range.rb +9 -0
  39. data/lib/smart_core/types/value/rational.rb +13 -0
  40. data/lib/smart_core/types/value/set.rb +13 -0
  41. data/lib/smart_core/types/value/string_io.rb +13 -0
  42. data/lib/smart_core/types/value/unbound_method.rb +9 -0
  43. data/lib/smart_core/types/version.rb +2 -1
  44. data/smart_types.gemspec +5 -4
  45. metadata +53 -17
  46. data/lib/smart_core/types/primitive/mult_checker.rb +0 -31
  47. data/lib/smart_core/types/primitive/nilable_checker.rb +0 -37
  48. data/lib/smart_core/types/primitive/sum_checker.rb +0 -31
@@ -2,7 +2,7 @@
2
2
 
3
3
  # @api private
4
4
  # @since 0.1.0
5
- class SmartCore::Types::Primitive::Factory::DefinitionContext
5
+ class SmartCore::Types::Primitive::MultFactory::DefinitionContext
6
6
  # @return [Proc, NilClass]
7
7
  #
8
8
  # @api private
@@ -23,7 +23,9 @@ class SmartCore::Types::Primitive::Factory::DefinitionContext
23
23
  # @api public
24
24
  # @since 0.1.0
25
25
  def define_caster(&caster)
26
- raise(SmartCore::ArgumentError, 'No caster definition block') unless block_given?
26
+ raise(SmartCore::Types::ArgumentError, 'No caster definition block') unless block_given?
27
27
  @type_caster = caster
28
28
  end
29
+
30
+ # TODO (0.x.0): invariant API
29
31
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ class SmartCore::Types::Primitive::MultValidator < SmartCore::Types::Primitive::SumValidator
6
+ require_relative 'mult_validator/result'
7
+
8
+ # @overload validate(value)
9
+ # @param value [Any]
10
+ # @return [SmartCore::Types::Primitive::MultValidator::Result]
11
+ #
12
+ # @api private
13
+ # @since 0.2.0
14
+
15
+ private
16
+
17
+ # @param validation [Block]
18
+ # @yieldparam [void]
19
+ # @yieldreturn [SmartCore::Engine::Atom]
20
+ # @return [SmartCore::Types::Primitive::MultValidator::Result]
21
+ #
22
+ # @api private
23
+ # @since 0.2.0
24
+ def compile_validation_result(&validation)
25
+ # NOTE: at this moment type sum does not support invariant checking
26
+ # TODO (0.x.0):
27
+ # @yieldreturn [SmartCore::Types::Primitive::Validator::Result]
28
+ # => and:
29
+ # SmartCore::Types::Primitive::MultValidator::Result.new(
30
+ # type, final_result.value, final_result.value.invariant_errors
31
+ # )
32
+ SmartCore::Types::Primitive::MultValidator::Result.new(type, yield.value)
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SmartCore::Types::Primitive::MultValidator
4
+ # @api private
5
+ # @since 0.2.0
6
+ class Result < SmartCore::Types::Primitive::SumValidator::Result
7
+ end
8
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  # @api private
4
4
  # @since 0.1.0
5
+ # @version 0.2.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
- type_checker = build_type_checker(type)
15
+ type_validator = build_type_validator(type)
14
16
  type_caster = build_type_caster(type)
15
- build_type(type, type_checker, type_caster)
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::NilableChecker]
25
+ # @return [SmartCore::Types::Primitive::NilableValidator]
22
26
  #
23
27
  # @api private
24
- # @since 0.1.0
25
- def build_type_checker(type)
26
- SmartCore::Types::Primitive::NilableChecker.new(type.checker)
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,25 @@ module SmartCore::Types::Primitive::NilableFactory
36
40
  end
37
41
 
38
42
  # @param type [SmartCore::Types::Primitive]
39
- # @param type_checker [SmartCore::Types::Primitive::NilableChecker]
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
- def build_type(type, type_checker, type_caster)
46
- Class.new(type.class).new(type_checker, type_caster)
59
+ # @version 0.2.0
60
+ def build_type(type, type_validator, type_caster)
61
+ Class.new(type.class).new(type.name, type_validator, type_caster)
47
62
  end
48
63
  end
49
64
  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
@@ -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
@@ -2,6 +2,7 @@
2
2
 
3
3
  # @api private
4
4
  # @since 0.1.0
5
+ # @version 0.2.0
5
6
  module SmartCore::Types::Primitive::SumFactory
6
7
  require_relative 'sum_factory/definition_context'
7
8
 
@@ -12,11 +13,14 @@ module SmartCore::Types::Primitive::SumFactory
12
13
  #
13
14
  # @api private
14
15
  # @since 0.1.0
16
+ # @version 0.2.0
15
17
  def create_type(types, type_definition)
16
18
  type_definitions = build_type_definitions(type_definition)
17
- type_checker = build_type_checker(types, type_definitions)
19
+ type_validator = build_type_validator(types, type_definitions)
18
20
  type_caster = build_type_caster(types, type_definitions)
19
- build_type(type_checker, type_caster)
21
+ build_type(type_validator, type_caster).tap do |type|
22
+ assign_type_validator(type, type_validator)
23
+ end
20
24
  end
21
25
 
22
26
  private
@@ -34,12 +38,12 @@ module SmartCore::Types::Primitive::SumFactory
34
38
 
35
39
  # @param types [Array<SmartCore::Types::Primtive>]
36
40
  # @param type_definitions [SmartCore::Types::Primitive::SumFactory::DefinitionContext]
37
- # @return [SmartCore::Types::Primitive::SumChecker]
41
+ # @return [SmartCore::Types::Primitive::SumValidator]
38
42
  #
39
43
  # @api private
40
- # @since 0.1.0
41
- def build_type_checker(types, type_definitions)
42
- SmartCore::Types::Primitive::SumChecker.new(*types.map(&:checker))
44
+ # @since 0.2.0
45
+ def build_type_validator(types, type_definitions)
46
+ SmartCore::Types::Primitive::SumValidator.new(*types.map(&:validator))
43
47
  end
44
48
 
45
49
  # @param types [Array<SmartCore::Types::Primtive>]
@@ -49,21 +53,32 @@ module SmartCore::Types::Primitive::SumFactory
49
53
  # @api private
50
54
  # @since 0.1.0
51
55
  def build_type_caster(types, type_definitions)
52
- if type_definitions.type_caster.nil?
56
+ if type_definitions.type_caster == nil
53
57
  SmartCore::Types::Primitive::UndefinedCaster.new
54
58
  else
55
59
  SmartCore::Types::Primitive::Caster.new(type_definitions.type_caster)
56
60
  end
57
61
  end
58
62
 
59
- # @param type_checker [SmartCore::Types::Primitive::SumChecker]
63
+ # @param type [SmartCore::Types::Primitive]
64
+ # @param type_validator [SmartCore::Types::Primitive::SumValidator]
65
+ # @return [void]
66
+ #
67
+ # @api private
68
+ # @since 0.2.0
69
+ def assign_type_validator(type, type_validator)
70
+ type_validator.___assign_type___(type)
71
+ end
72
+
73
+ # @param type_validator [SmartCore::Types::Primitive::SumValidator)]
60
74
  # @param type_caster [SmartCore::Types::Primitive::Caster]
61
75
  # @return [SmartCore::Types::Primitive]
62
76
  #
63
77
  # @api private
64
78
  # @since 0.1.0
65
- def build_type(type_checker, type_caster)
66
- SmartCore::Types::Primitive.new(type_checker, type_caster)
79
+ # @version 0.2.0
80
+ def build_type(type_validator, type_caster)
81
+ SmartCore::Types::Primitive.new(nil, type_validator, type_caster)
67
82
  end
68
83
  end
69
84
  end
@@ -23,7 +23,9 @@ class SmartCore::Types::Primitive::SumFactory::DefinitionContext
23
23
  # @api public
24
24
  # @since 0.1.0
25
25
  def define_caster(&caster)
26
- raise(SmartCore::ArgumentError, 'No caster definition block') unless block_given?
26
+ raise(SmartCore::Types::ArgumentError, 'No caster definition block') unless block_given?
27
27
  @type_caster = caster
28
28
  end
29
+
30
+ # TODO (0.x.0): invariant API
29
31
  end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ class SmartCore::Types::Primitive::SumValidator
6
+ require_relative 'sum_validator/result'
7
+
8
+ # @param validators [Array<SmartCore::Types::Primitive::Validator>]
9
+ # @return [void]
10
+ #
11
+ # @api private
12
+ # @since 0.2.0
13
+ def initialize(*validators)
14
+ @type = nil
15
+ @validators = validators
16
+ end
17
+
18
+ # @param type [SmartCore::Types::Primitive]
19
+ # @return [void]
20
+ #
21
+ # @api private
22
+ # @since 0.2.0
23
+ def ___assign_type___(type)
24
+ @type = type
25
+ end
26
+
27
+ # @return [Boolean]
28
+ #
29
+ # @api private
30
+ # @since 0.2.0
31
+ def valid?(value)
32
+ # NOTE: at this moment type sum does not support invariant checking
33
+ # TODO (0.x.0):
34
+ # validators.any? { |validator| validator.valid?(value) }
35
+ validators.any? { |validator| validator.type_checker.call(value) }
36
+ end
37
+
38
+ # @param value [Any]
39
+ # @return [SmartCore::Types::Primitive::SumValidator::Result]
40
+ #
41
+ # @api private
42
+ # @since 0.2.0
43
+ def validate(value)
44
+ compile_validation_result do
45
+ SmartCore::Engine::Atom.new.tap do |result|
46
+ validators.each do |validator|
47
+ # NOTE: at this moment type sum does not support invariant checking
48
+ # TODO (0.x.0):
49
+ # result.swap { validator.validate(value) }
50
+ # break if result.value.success?
51
+ result.swap { validator.type_checker.call(value) }
52
+ break if result.value # => boolean
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ # @param value [Any]
59
+ # @return [void]
60
+ #
61
+ # @raise [SmartCore::Types::TypeError]
62
+ #
63
+ # @api private
64
+ # @since 0.2.0
65
+ def validate!(value)
66
+ return if valid?(value)
67
+ raise(SmartCore::Types::TypeError, 'Invalid type')
68
+ end
69
+
70
+ private
71
+
72
+ # @return [SmartCore::Types::Primitive]
73
+ #
74
+ # @api private
75
+ # @since 0.2.0
76
+ attr_reader :type
77
+
78
+ # @return [Array<SmartCore::Types::Primitive::Validator>]
79
+ #
80
+ # @api private
81
+ # @since 0.2.0
82
+ attr_reader :validators
83
+
84
+ # @param validation [Block]
85
+ # @yieldparam [void]
86
+ # @yieldreturn [SmartCore::Engine::Atom]
87
+ # @return [SmartCore::Types::Primitive::SumValidator::Result]
88
+ #
89
+ # @api private
90
+ # @since 0.2.0
91
+ def compile_validation_result(&validation)
92
+ # NOTE: at this moment type sum does not support invariant checking
93
+ # TODO (0.x.0):
94
+ # @yieldreturn [SmartCore::Types::Primitive::Validator::Result]
95
+ # => and:
96
+ # SmartCore::Types::Primitive::SumValidator::Result.new(
97
+ # type, final_result.value, final_result.value.invariant_errors
98
+ # )
99
+ SmartCore::Types::Primitive::SumValidator::Result.new(type, yield.value)
100
+ end
101
+ end