smart_types 0.1.0.alpha5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +10 -1
  3. data/CHANGELOG.md +46 -0
  4. data/Gemfile.lock +80 -52
  5. data/README.md +437 -27
  6. data/Rakefile +1 -1
  7. data/bin/console +2 -2
  8. data/lib/smart_core/types.rb +2 -0
  9. data/lib/smart_core/types/errors.rb +16 -0
  10. data/lib/smart_core/types/primitive.rb +109 -24
  11. data/lib/smart_core/types/primitive/caster.rb +5 -2
  12. data/lib/smart_core/types/primitive/checker.rb +5 -2
  13. data/lib/smart_core/types/primitive/factory.rb +105 -8
  14. data/lib/smart_core/types/primitive/factory/definition_context.rb +142 -6
  15. data/lib/smart_core/types/primitive/factory/runtime_type_builder.rb +53 -0
  16. data/lib/smart_core/types/primitive/invariant_control.rb +67 -0
  17. data/lib/smart_core/types/primitive/invariant_control/chain.rb +61 -0
  18. data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
  19. data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
  20. data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
  21. data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
  22. data/lib/smart_core/types/primitive/invariant_control/single.rb +57 -0
  23. data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
  24. data/lib/smart_core/types/primitive/mult_factory.rb +59 -10
  25. data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +27 -3
  26. data/lib/smart_core/types/primitive/mult_validator.rb +42 -0
  27. data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
  28. data/lib/smart_core/types/primitive/nilable_factory.rb +31 -9
  29. data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
  30. data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
  31. data/lib/smart_core/types/primitive/runtime_attributes_checker.rb +77 -0
  32. data/lib/smart_core/types/primitive/sum_factory.rb +59 -10
  33. data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +26 -2
  34. data/lib/smart_core/types/primitive/sum_validator.rb +117 -0
  35. data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
  36. data/lib/smart_core/types/primitive/undefined_caster.rb +7 -5
  37. data/lib/smart_core/types/primitive/validator.rb +93 -0
  38. data/lib/smart_core/types/primitive/validator/result.rb +78 -0
  39. data/lib/smart_core/types/protocol.rb +7 -0
  40. data/lib/smart_core/types/protocol/instance_of.rb +19 -0
  41. data/lib/smart_core/types/system.rb +21 -5
  42. data/lib/smart_core/types/value.rb +16 -0
  43. data/lib/smart_core/types/value/array.rb +3 -0
  44. data/lib/smart_core/types/value/big_decimal.rb +31 -0
  45. data/lib/smart_core/types/value/boolean.rb +3 -0
  46. data/lib/smart_core/types/value/class.rb +3 -0
  47. data/lib/smart_core/types/value/comparable.rb +13 -0
  48. data/lib/smart_core/types/value/date.rb +24 -0
  49. data/lib/smart_core/types/value/date_time.rb +24 -0
  50. data/lib/smart_core/types/value/enumerable.rb +13 -0
  51. data/lib/smart_core/types/value/enumerator.rb +13 -0
  52. data/lib/smart_core/types/value/enumerator_chain.rb +13 -0
  53. data/lib/smart_core/types/value/float.rb +9 -2
  54. data/lib/smart_core/types/value/hash.rb +11 -1
  55. data/lib/smart_core/types/value/integer.rb +13 -3
  56. data/lib/smart_core/types/value/io.rb +13 -0
  57. data/lib/smart_core/types/value/method.rb +9 -0
  58. data/lib/smart_core/types/value/module.rb +3 -0
  59. data/lib/smart_core/types/value/nil.rb +3 -3
  60. data/lib/smart_core/types/value/numeric.rb +16 -3
  61. data/lib/smart_core/types/value/proc.rb +14 -1
  62. data/lib/smart_core/types/value/range.rb +9 -0
  63. data/lib/smart_core/types/value/rational.rb +13 -0
  64. data/lib/smart_core/types/value/set.rb +21 -0
  65. data/lib/smart_core/types/value/string.rb +10 -1
  66. data/lib/smart_core/types/value/string_io.rb +15 -0
  67. data/lib/smart_core/types/value/symbol.rb +10 -1
  68. data/lib/smart_core/types/value/text.rb +21 -4
  69. data/lib/smart_core/types/value/time.rb +24 -0
  70. data/lib/smart_core/types/value/time_based.rb +32 -0
  71. data/lib/smart_core/types/value/unbound_method.rb +9 -0
  72. data/lib/smart_core/types/variadic.rb +7 -0
  73. data/lib/smart_core/types/variadic/tuple.rb +23 -0
  74. data/lib/smart_core/types/version.rb +2 -2
  75. data/smart_types.gemspec +6 -5
  76. metadata +69 -23
  77. data/.travis.yml +0 -20
  78. data/lib/smart_core/types/primitive/mult_checker.rb +0 -31
  79. data/lib/smart_core/types/primitive/nilable_checker.rb +0 -37
  80. data/lib/smart_core/types/primitive/sum_checker.rb +0 -31
  81. data/lib/smart_core/types/system/definition_dsl.rb +0 -40
  82. data/lib/smart_core/types/system/producer_dsl.rb +0 -30
@@ -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.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
- 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,32 @@ module SmartCore::Types::Primitive::NilableFactory
36
40
  end
37
41
 
38
42
  # @param type [SmartCore::Types::Primitive]
39
- # @param type_checler [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.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,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
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.3.0
5
+ class SmartCore::Types::Primitive::RuntimeAttributesChecker
6
+ # @return [Proc]
7
+ #
8
+ # @api private
9
+ # @since 0.3.0
10
+ ATTRIBUTES_IS_NOT_ALLOWED_CHECKER = proc do |attrs|
11
+ attrs.empty? || raise(SmartCore::Types::RuntimeAttriburtesUnsupportedError, <<~ERROR_MESSAGE)
12
+ This type has no support for runtime attributes.
13
+ ERROR_MESSAGE
14
+ end.freeze
15
+
16
+ # @param attributes_checker [Proc]
17
+ # @return [void]
18
+ #
19
+ # @api private
20
+ # @since 0.3.0
21
+ def initialize(attributes_checker)
22
+ @type = nil
23
+ @attributes_checker = attributes_checker || ATTRIBUTES_IS_NOT_ALLOWED_CHECKER
24
+ end
25
+
26
+ # @param type [SmartCore::Types::Primitive]
27
+ # @return [SmartCore::Types::Primitive::RuntimeAttributesChecker]
28
+ #
29
+ # @api private
30
+ # @since 0.3.0
31
+ def ___copy_for___(type)
32
+ self.class.new(attributes_checker).tap do |instance_copy|
33
+ instance_copy.___assign_type___(type)
34
+ end
35
+ end
36
+
37
+ # @param type [SmartCore::Types::Primitive]
38
+ # @return [void]
39
+ #
40
+ # @api private
41
+ # @since 0.3.0
42
+ def ___assign_type___(type)
43
+ @type = type
44
+ end
45
+
46
+ # @param runtime_attributes [Array<Any>]
47
+ # @return [void]
48
+ #
49
+ # @raise [SmartCore::Types::IncorrectRuntimeAttributesError]
50
+ #
51
+ # @api private
52
+ # @since 0.3.0
53
+ def check!(runtime_attributes)
54
+ unless !!attributes_checker.call(runtime_attributes)
55
+ # TODO (0.x.0):
56
+ # Full type name (with type category; and delegated to the type object).
57
+ # (main problem: sum and mult types has no type name and type category)
58
+ raise(SmartCore::Types::IncorrectRuntimeAttributesError, <<~ERROR_MESSAGE)
59
+ Incorrect runtime attributes for #{type.name}.
60
+ ERROR_MESSAGE
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ # @return [SmartCore::Types::Primitive]
67
+ #
68
+ # @api private
69
+ # @since 0.3.0
70
+ attr_reader :type
71
+
72
+ # @return [Proc]
73
+ #
74
+ # @api private
75
+ # @since 0.3.0
76
+ attr_reader :attributes_checker
77
+ 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::SumFactory
6
7
  require_relative 'sum_factory/definition_context'
7
8
 
@@ -12,11 +13,20 @@ module SmartCore::Types::Primitive::SumFactory
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
- type_checker = build_type_checker(types, type_definitions)
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(type_checker, type_caster)
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::SumFactory
32
42
  end
33
43
  end
34
44
 
45
+ # @param type_definitions [SmartCore::Types::Primitive::SumFactory::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::SumFactory::DefinitionContext]
37
- # @return [SmartCore::Types::Primitive::SumChecker]
58
+ # @return [SmartCore::Types::Primitive::SumValidator]
38
59
  #
39
60
  # @api private
40
- # @since 0.1.0
41
- def build_type_checker(types, type_definitions)
42
- SmartCore::Types::Primitive::SumChecker.new(*types.map(&:checker))
61
+ # @since 0.2.0
62
+ def build_type_validator(types, type_definitions)
63
+ SmartCore::Types::Primitive::SumValidator.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::SumFactory
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.nil?
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 type_checker [SmartCore::Types::Primitive::SumChecker]
80
+ # @param type [SmartCore::Types::Primitive]
81
+ # @param type_validator [SmartCore::Types::Primitive::SumValidator]
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::SumValidator)]
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
- def build_type(type_checker, type_caster)
66
- SmartCore::Types::Primitive.new(type_checker, type_caster)
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
@@ -2,19 +2,28 @@
2
2
 
3
3
  # @api private
4
4
  # @since 0.1.0
5
+ # @version 0.3.0
5
6
  class SmartCore::Types::Primitive::SumFactory::DefinitionContext
6
- # @return [Proc]
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::SumFactory::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 'No caster definition block' unless block_given?
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