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.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -1
  3. data/CHANGELOG.md +57 -0
  4. data/Gemfile.lock +71 -58
  5. data/LICENSE.txt +1 -1
  6. data/README.md +363 -34
  7. data/Rakefile +0 -1
  8. data/bin/console +2 -2
  9. data/lib/smart_core/types/errors.rb +12 -0
  10. data/lib/smart_core/types/primitive/caster.rb +5 -2
  11. data/lib/smart_core/types/primitive/checker.rb +5 -2
  12. data/lib/smart_core/types/primitive/factory/definition_context.rb +140 -4
  13. data/lib/smart_core/types/primitive/factory/runtime_type_builder.rb +53 -0
  14. data/lib/smart_core/types/primitive/factory.rb +104 -7
  15. data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
  16. data/lib/smart_core/types/primitive/invariant_control/chain.rb +61 -0
  17. data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
  18. data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
  19. data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
  20. data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
  21. data/lib/smart_core/types/primitive/invariant_control/single.rb +57 -0
  22. data/lib/smart_core/types/primitive/invariant_control.rb +67 -0
  23. data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +26 -2
  24. data/lib/smart_core/types/primitive/mult_factory.rb +59 -10
  25. data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
  26. data/lib/smart_core/types/primitive/mult_validator.rb +42 -0
  27. data/lib/smart_core/types/primitive/nilable_factory.rb +31 -9
  28. data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
  29. data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
  30. data/lib/smart_core/types/primitive/runtime_attributes_checker.rb +77 -0
  31. data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +25 -1
  32. data/lib/smart_core/types/primitive/sum_factory.rb +59 -10
  33. data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
  34. data/lib/smart_core/types/primitive/sum_validator.rb +117 -0
  35. data/lib/smart_core/types/primitive/undefined_caster.rb +4 -1
  36. data/lib/smart_core/types/primitive/validator/result.rb +78 -0
  37. data/lib/smart_core/types/primitive/validator.rb +93 -0
  38. data/lib/smart_core/types/primitive.rb +99 -15
  39. data/lib/smart_core/types/protocol/instance_of.rb +19 -0
  40. data/lib/smart_core/types/protocol.rb +7 -0
  41. data/lib/smart_core/types/value/array.rb +3 -0
  42. data/lib/smart_core/types/value/big_decimal.rb +4 -1
  43. data/lib/smart_core/types/value/boolean.rb +3 -0
  44. data/lib/smart_core/types/value/class.rb +3 -0
  45. data/lib/smart_core/types/value/date.rb +3 -0
  46. data/lib/smart_core/types/value/date_time.rb +3 -0
  47. data/lib/smart_core/types/value/enumerable.rb +1 -1
  48. data/lib/smart_core/types/value/enumerator.rb +13 -0
  49. data/lib/smart_core/types/value/enumerator_chain.rb +13 -0
  50. data/lib/smart_core/types/value/float.rb +3 -0
  51. data/lib/smart_core/types/value/hash.rb +3 -0
  52. data/lib/smart_core/types/value/integer.rb +3 -0
  53. data/lib/smart_core/types/value/io.rb +13 -0
  54. data/lib/smart_core/types/value/method.rb +9 -0
  55. data/lib/smart_core/types/value/module.rb +3 -0
  56. data/lib/smart_core/types/value/nil.rb +0 -2
  57. data/lib/smart_core/types/value/numeric.rb +3 -0
  58. data/lib/smart_core/types/value/proc.rb +3 -0
  59. data/lib/smart_core/types/value/range.rb +9 -0
  60. data/lib/smart_core/types/value/rational.rb +13 -0
  61. data/lib/smart_core/types/value/set.rb +21 -0
  62. data/lib/smart_core/types/value/string.rb +3 -0
  63. data/lib/smart_core/types/value/string_io.rb +15 -0
  64. data/lib/smart_core/types/value/symbol.rb +3 -0
  65. data/lib/smart_core/types/value/text.rb +6 -4
  66. data/lib/smart_core/types/value/time.rb +3 -0
  67. data/lib/smart_core/types/value/time_based.rb +8 -5
  68. data/lib/smart_core/types/value/unbound_method.rb +9 -0
  69. data/lib/smart_core/types/value.rb +9 -0
  70. data/lib/smart_core/types/variadic/array_of.rb +23 -0
  71. data/lib/smart_core/types/variadic/enum.rb +11 -0
  72. data/lib/smart_core/types/variadic/tuple.rb +23 -0
  73. data/lib/smart_core/types/variadic.rb +10 -0
  74. data/lib/smart_core/types/version.rb +2 -1
  75. data/lib/smart_core/types.rb +2 -0
  76. data/smart_types.gemspec +5 -4
  77. metadata +61 -18
  78. data/.travis.yml +0 -21
  79. data/lib/smart_core/types/primitive/mult_checker.rb +0 -31
  80. data/lib/smart_core/types/primitive/nilable_checker.rb +0 -37
  81. data/lib/smart_core/types/primitive/sum_checker.rb +0 -31
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Class) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Class)
@@ -2,8 +2,11 @@
2
2
 
3
3
  require 'date'
4
4
 
5
+ using SmartCore::Ext::BasicObjectAsObject
6
+
5
7
  # @api public
6
8
  # @since 0.1.0
9
+ # @version 0.3.0
7
10
  SmartCore::Types::Value.define_type(:Date) do |type|
8
11
  type.define_checker do |value|
9
12
  value.is_a?(::Date) || value == ::Date::Infinity
@@ -2,8 +2,11 @@
2
2
 
3
3
  require 'date'
4
4
 
5
+ using SmartCore::Ext::BasicObjectAsObject
6
+
5
7
  # @api public
6
8
  # @since 0.1.0
9
+ # @version 0.3.0
7
10
  SmartCore::Types::Value.define_type(:DateTime) do |type|
8
11
  type.define_checker do |value|
9
12
  value.is_a?(::DateTime) || value == ::DateTime::Infinity
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # @api public
4
- # @since 0.1.0
4
+ # @since 0.x.0
5
5
  SmartCore::Types::Value.define_type(:Enumerable) do |type|
6
6
  type.define_checker do |value|
7
7
  # TODO: realize
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.x.0
5
+ SmartCore::Types::Value.define_type(:Enumerator) 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
@@ -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
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Float) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Float)
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Hash) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Hash)
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Integer) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Integer)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.x.0
5
+ SmartCore::Types::Value.define_type(:IO) 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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.x.0
5
+ SmartCore::Types::Value.define_type(:Method) do |type|
6
+ type.define_checker do |value|
7
+ # TODO: realize
8
+ end
9
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Module) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Module)
@@ -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
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Numeric) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Numeric)
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Proc) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Proc)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.x.0
5
+ SmartCore::Types::Value.define_type(:Range) do |type|
6
+ type.define_checker do |value|
7
+ # TODO: realize
8
+ end
9
+ 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(:Rational) 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
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ using SmartCore::Ext::BasicObjectAsObject
6
+
7
+ # @api public
8
+ # @since 0.3.0
9
+ SmartCore::Types::Value.define_type(:Set) do |type|
10
+ type.define_checker do |value|
11
+ value.is_a?(::Set)
12
+ end
13
+
14
+ type.define_caster do |value|
15
+ begin
16
+ ::Set.new(SmartCore::Types::Value::Array.cast(value))
17
+ rescue ::ArgumentError, ::NoMethodError
18
+ raise(SmartCore::Types::TypeCastingError, 'Non-castable to Set')
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:String) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::String)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
5
+ # @api public
6
+ # @since 0.x.0
7
+ SmartCore::Types::Value.define_type(:StringIO) do |type|
8
+ type.define_checker do |value|
9
+ # TODO: realize
10
+ end
11
+
12
+ type.define_caster do |value|
13
+ # TODO: realize
14
+ end
15
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
3
5
  # @api public
4
6
  # @since 0.1.0
7
+ # @version 0.3.0
5
8
  SmartCore::Types::Value.define_type(:Symbol) do |type|
6
9
  type.define_checker do |value|
7
10
  value.is_a?(::Symbol)
@@ -2,10 +2,12 @@
2
2
 
3
3
  # @api public
4
4
  # @since 0.1.0
5
- SmartCore::Types::Value::Text = SmartCore::Types::System.type_sum(
6
- SmartCore::Types::Value::String,
7
- SmartCore::Types::Value::Symbol
8
- ) do |type|
5
+ # @version 0.3.0
6
+ SmartCore::Types::Value.define_type(:Text) do |type|
7
+ type.define_checker do |value|
8
+ SmartCore::Types::Value::String.valid?(value) || SmartCore::Types::Value::Symbol.valid?(value)
9
+ end
10
+
9
11
  type.define_caster do |value|
10
12
  next value if SmartCore::Types::Value::String.valid?(value)
11
13
  next value if SmartCore::Types::Value::Symbol.valid?(value)
@@ -2,8 +2,11 @@
2
2
 
3
3
  require 'time'
4
4
 
5
+ using SmartCore::Ext::BasicObjectAsObject
6
+
5
7
  # @api public
6
8
  # @since 0.1.0
9
+ # @version 0.3.0
7
10
  SmartCore::Types::Value.define_type(:Time) do |type|
8
11
  type.define_checker do |value|
9
12
  value.is_a?(::Time)
@@ -2,11 +2,14 @@
2
2
 
3
3
  # @api public
4
4
  # @since 0.1.0
5
- SmartCore::Types::Value::TimeBased = SmartCore::Types::System.type_sum(
6
- SmartCore::Types::Value::Time,
7
- SmartCore::Types::Value::DateTime,
8
- SmartCore::Types::Value::Date
9
- ) do |type|
5
+ # @version 0.3.0
6
+ SmartCore::Types::Value.define_type(:TimeBased) do |type|
7
+ type.define_checker do |value|
8
+ SmartCore::Types::Value::Time.valid?(value) ||
9
+ SmartCore::Types::Value::DateTime.valid?(value) ||
10
+ SmartCore::Types::Value::Date.valid?(value)
11
+ end
12
+
10
13
  type.define_caster do |value|
11
14
  next value if SmartCore::Types::Value::Time.valid?(value)
12
15
  next value if SmartCore::Types::Value::DateTime.valid?(value)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.x.0
5
+ SmartCore::Types::Value.define_type(:UnboundMethod) do |type|
6
+ type.define_checker do |value|
7
+ # TODO: realize
8
+ end
9
+ 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,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
5
+ # @api public
6
+ # @since 0.6.0
7
+ SmartCore::Types::Variadic.define_type(:ArrayOf) do |type|
8
+ type.define_caster { |value| SmartCore::Types::Value::Array.cast(value) }
9
+
10
+ type.runtime_attributes_checker do |runtime_attrs|
11
+ runtime_attrs.any? && runtime_attrs.all? do |runtime_attr|
12
+ runtime_attr.is_a?(::Class)
13
+ end
14
+ end
15
+
16
+ type.define_checker do |value, expected_types|
17
+ next false unless SmartCore::Types::Value::Array.valid?(value)
18
+
19
+ value.all? do |elem|
20
+ expected_types.any? { |expected_type| elem.is_a?(expected_type) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
5
+ # @api public
6
+ # @since 0.5.0
7
+ SmartCore::Types::Variadic.define_type(:Enum) do |type|
8
+ type.runtime_attributes_checker(&:any?)
9
+
10
+ type.define_checker { |value, enum| enum.include?(value) }
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ using SmartCore::Ext::BasicObjectAsObject
4
+
5
+ # @api public
6
+ # @since 0.3.0
7
+ SmartCore::Types::Variadic.define_type(:Tuple) do |type|
8
+ type.runtime_attributes_checker do |runtime_attrs|
9
+ runtime_attrs.any? && runtime_attrs.all? do |runtime_attr|
10
+ runtime_attr.is_a?(::Class)
11
+ end
12
+ end
13
+
14
+ type.define_checker do |value, tuple_signature|
15
+ next false unless SmartCore::Types::Value::Array.valid?(value)
16
+ next false if value.size != tuple_signature.size
17
+
18
+ value.each_with_index.all? do |tuple_val, tuple_val_index|
19
+ expected_tuple_type = tuple_signature.at(tuple_val_index)
20
+ tuple_val.is_a?(expected_tuple_type)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.3.0
5
+ # @version 0.6.0
6
+ class SmartCore::Types::Variadic < SmartCore::Types::Primitive
7
+ require_relative 'variadic/array_of'
8
+ require_relative 'variadic/enum'
9
+ require_relative 'variadic/tuple'
10
+ end