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,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ class SmartCore::Types::Primitive::InvariantControl::Factory::ChainDefinitionContext
6
+ # @return [SmartCore::Types::Primitive::InvariantControl::Chain]
7
+ #
8
+ # @api private
9
+ # @since 0.2.0
10
+ attr_reader :___chain___
11
+
12
+ # @param chain_name [String]
13
+ # @return [void]
14
+ #
15
+ # @api privae
16
+ # @since 0.2.0
17
+ def initialize(chain_name)
18
+ @___chain___ = SmartCore::Types::Primitive::InvariantControl::Chain.new(chain_name)
19
+ end
20
+
21
+ # @param invariant_name [String, Symbol]
22
+ # @param invariant_definition [Block]
23
+ # @return [void]
24
+ #
25
+ # @api private
26
+ # @since 0.2.0
27
+ def invariant(invariant_name, &invariant_definition)
28
+ SmartCore::Types::Primitive::Factory::DefinitionContext.vaildate_invariant_attributes!(
29
+ invariant_name,
30
+ &invariant_definition
31
+ )
32
+
33
+ ___chain___.add_invariant(
34
+ SmartCore::Types::Primitive::InvariantControl::Single.create(
35
+ invariant_name, invariant_definition
36
+ )
37
+ )
38
+ end
39
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ class SmartCore::Types::Primitive::InvariantControl::Result
6
+ # @return [Array<SmartCore::Types::Primitive::InvariantControl::Chain::Result>]
7
+ #
8
+ # @api private
9
+ # @since 0.2.0
10
+ attr_reader :chain_results
11
+
12
+ # @return [Array<SmartCore::Types::Primitive::InvariantControl::Single::Result>]
13
+ #
14
+ # @api private
15
+ # @since 0.2.0
16
+ attr_reader :single_results
17
+
18
+ # @param invariant_control [SmartCore::Types::Primitive::InvariantControl]
19
+ # @param checked_value [Any]
20
+ # @return [void]
21
+ #
22
+ # @api private
23
+ # @since 0.2.0
24
+ def initialize(invariant_control, checked_value)
25
+ @invariant_control = invariant_control
26
+ @checked_value = checked_value
27
+ @chain_results = []
28
+ @single_results = []
29
+ end
30
+
31
+ # @return [Array<String>]
32
+ #
33
+ # @api private
34
+ # @since 0.2.0
35
+ def invariant_errors
36
+ collect_invariant_errors
37
+ end
38
+
39
+ # @return [Boolean]
40
+ #
41
+ # @api private
42
+ # @since 0.2.0
43
+ def success?
44
+ chain_results.all(&:success?) && single_results.all?(&:success?)
45
+ end
46
+
47
+ # @return [Boolean]
48
+ #
49
+ # @api private
50
+ # @since 0.2.0
51
+ def failure?
52
+ !success?
53
+ end
54
+
55
+ # @param result [SmartCore::Types::Primitive::InvariantControl::Chain::Result]
56
+ # @return [void]
57
+ #
58
+ # @api private
59
+ # @since 0.2.0
60
+ def add_chain_result(result)
61
+ chain_results << result
62
+ end
63
+
64
+ # @param result [SmartCore::Types::Primitive::InvariantControl::Single::Result]
65
+ # @return [void]
66
+ #
67
+ # @api private
68
+ # @since 0.2.0
69
+ def add_single_result(result)
70
+ single_results << result
71
+ end
72
+
73
+ private
74
+
75
+ # @return [SmartCore::Types::Primitive::InvariantControl]
76
+ #
77
+ # @api private
78
+ # @since 0.2.0
79
+ attr_reader :invariant_control
80
+
81
+ # @return [Any]
82
+ #
83
+ # @api private
84
+ # @since 0.2.0
85
+ attr_reader :checked_value
86
+
87
+ # @return [Array<String>]
88
+ #
89
+ # @api private
90
+ # @since 0.2.0
91
+ def collect_invariant_errors
92
+ [].tap do |invariant_errors|
93
+ # collect invariant errors from invariant chains
94
+ chain_results.select(&:failure?).each do |chain_result|
95
+ invariant_errors.concat(chain_result.error_codes)
96
+ end
97
+
98
+ # collect invariant errors from single invariants
99
+ single_results.select(&:failure?).each do |single_result|
100
+ invariant_errors.concat(single_result.error_codes)
101
+ end
102
+ end.tap(&:freeze)
103
+ end
104
+ end
@@ -0,0 +1,57 @@
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::Single
7
+ require_relative 'single/result'
8
+
9
+ class << self
10
+ # @param name [String, Symbol]
11
+ # @param invariant_checker [Proc]
12
+ # @return [SmartCore::Types::Primitive::InvariantControl::Single]
13
+ #
14
+ # @api private
15
+ # @since 0.2.0
16
+ def create(name, invariant_checker)
17
+ new(name.to_s, invariant_checker)
18
+ end
19
+ end
20
+
21
+ # @return [String]
22
+ #
23
+ # @api private
24
+ # @since 0.2.0
25
+ attr_reader :name
26
+
27
+ # @param name [String]
28
+ # @param invariant_checker [Proc]
29
+ # @return [void]
30
+ #
31
+ # @api private
32
+ # @since 0.2.0
33
+ def initialize(name, invariant_checker)
34
+ @name = name.dup.tap(&:freeze)
35
+ @invariant_checker = invariant_checker
36
+ end
37
+
38
+ # @param value [Any]
39
+ # @param runtime_attributes [Array<Any>]
40
+ # @return [SmartCore::Types::Primitive::InvariantControl::Single::Result]
41
+ #
42
+ # @api private
43
+ # @since 0.2.0
44
+ # @version 0.3.0
45
+ def check(value, runtime_attributes)
46
+ validation_result = !!invariant_checker.call(value, runtime_attributes)
47
+ Result.new(self, value, validation_result)
48
+ end
49
+
50
+ private
51
+
52
+ # @return [Proc]
53
+ #
54
+ # @api private
55
+ # @since 0.2.0
56
+ attr_reader :invariant_checker
57
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ class SmartCore::Types::Primitive::InvariantControl::Single::Result
6
+ # @return [SmartCore::Types::Primitive::InvariantControl::Single]
7
+ #
8
+ # @api private
9
+ # @since 0.2.0
10
+ attr_reader :invariant
11
+
12
+ # @return [Any]
13
+ #
14
+ # @api private
15
+ # @since 0.2.0
16
+ attr_reader :checked_value
17
+
18
+ # @param invariant [SmartCore::Types::Primitive::InvariantControl::Single]
19
+ # @param checked_value [Any]
20
+ # @param is_valid_check [Boolean]
21
+ # @return [void]
22
+ #
23
+ # @api private
24
+ # @since 0.2.0
25
+ def initialize(invariant, checked_value, is_valid_check)
26
+ @invariant = invariant
27
+ @checked_value = checked_value
28
+ @is_valid_check = is_valid_check
29
+ end
30
+
31
+ # @return [Boolean]
32
+ #
33
+ # @api private
34
+ # @since 0.2.0
35
+ def success?
36
+ is_valid_check
37
+ end
38
+
39
+ # @return [Boolean]
40
+ #
41
+ # @api private
42
+ # @since 0.2.0
43
+ def failure?
44
+ !success?
45
+ end
46
+
47
+ # @return [Array<String>]
48
+ #
49
+ # @api private
50
+ # @since 0.2.0
51
+ def error_codes
52
+ success? ? [] : [invariant.name]
53
+ end
54
+
55
+ private
56
+
57
+ # @return [Boolean]
58
+ #
59
+ # @api private
60
+ # @since 0.2.0
61
+ attr_reader :is_valid_check
62
+ alias_method :valid_check?, :is_valid_check
63
+ 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
- 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::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::MultChecker]
58
+ # @return [SmartCore::Types::Primitive::MultValidator]
38
59
  #
39
60
  # @api private
40
- # @since 0.1.0
41
- def build_type_checker(types, type_definitions)
42
- SmartCore::Types::Primitive::MultChecker.new(*types.map(&:checker))
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.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::MultChecker]
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
- 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
- class SmartCore::Types::Primitive::Factory::DefinitionContext
6
- # @return [Proc]
5
+ # @version 0.3.0
6
+ class SmartCore::Types::Primitive::MultFactory::DefinitionContext
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 '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
@@ -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