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.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -1
- data/CHANGELOG.md +57 -0
- data/Gemfile.lock +71 -58
- data/LICENSE.txt +1 -1
- data/README.md +363 -34
- data/Rakefile +0 -1
- data/bin/console +2 -2
- data/lib/smart_core/types/errors.rb +12 -0
- data/lib/smart_core/types/primitive/caster.rb +5 -2
- data/lib/smart_core/types/primitive/checker.rb +5 -2
- data/lib/smart_core/types/primitive/factory/definition_context.rb +140 -4
- data/lib/smart_core/types/primitive/factory/runtime_type_builder.rb +53 -0
- data/lib/smart_core/types/primitive/factory.rb +104 -7
- data/lib/smart_core/types/primitive/invariant_control/chain/result.rb +64 -0
- data/lib/smart_core/types/primitive/invariant_control/chain.rb +61 -0
- data/lib/smart_core/types/primitive/invariant_control/factory/chain_definition_context.rb +39 -0
- data/lib/smart_core/types/primitive/invariant_control/factory.rb +54 -0
- data/lib/smart_core/types/primitive/invariant_control/result.rb +104 -0
- data/lib/smart_core/types/primitive/invariant_control/single/result.rb +63 -0
- data/lib/smart_core/types/primitive/invariant_control/single.rb +57 -0
- data/lib/smart_core/types/primitive/invariant_control.rb +67 -0
- data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +26 -2
- data/lib/smart_core/types/primitive/mult_factory.rb +59 -10
- data/lib/smart_core/types/primitive/mult_validator/result.rb +8 -0
- data/lib/smart_core/types/primitive/mult_validator.rb +42 -0
- data/lib/smart_core/types/primitive/nilable_factory.rb +31 -9
- data/lib/smart_core/types/primitive/nilable_validator/result.rb +78 -0
- data/lib/smart_core/types/primitive/nilable_validator.rb +83 -0
- data/lib/smart_core/types/primitive/runtime_attributes_checker.rb +77 -0
- data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +25 -1
- data/lib/smart_core/types/primitive/sum_factory.rb +59 -10
- data/lib/smart_core/types/primitive/sum_validator/result.rb +100 -0
- data/lib/smart_core/types/primitive/sum_validator.rb +117 -0
- data/lib/smart_core/types/primitive/undefined_caster.rb +4 -1
- data/lib/smart_core/types/primitive/validator/result.rb +78 -0
- data/lib/smart_core/types/primitive/validator.rb +93 -0
- data/lib/smart_core/types/primitive.rb +99 -15
- data/lib/smart_core/types/protocol/instance_of.rb +19 -0
- data/lib/smart_core/types/protocol.rb +7 -0
- data/lib/smart_core/types/value/array.rb +3 -0
- data/lib/smart_core/types/value/big_decimal.rb +4 -1
- data/lib/smart_core/types/value/boolean.rb +3 -0
- data/lib/smart_core/types/value/class.rb +3 -0
- data/lib/smart_core/types/value/date.rb +3 -0
- data/lib/smart_core/types/value/date_time.rb +3 -0
- data/lib/smart_core/types/value/enumerable.rb +1 -1
- 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/float.rb +3 -0
- data/lib/smart_core/types/value/hash.rb +3 -0
- data/lib/smart_core/types/value/integer.rb +3 -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/module.rb +3 -0
- data/lib/smart_core/types/value/nil.rb +0 -2
- data/lib/smart_core/types/value/numeric.rb +3 -0
- data/lib/smart_core/types/value/proc.rb +3 -0
- 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 +21 -0
- data/lib/smart_core/types/value/string.rb +3 -0
- data/lib/smart_core/types/value/string_io.rb +15 -0
- data/lib/smart_core/types/value/symbol.rb +3 -0
- data/lib/smart_core/types/value/text.rb +6 -4
- data/lib/smart_core/types/value/time.rb +3 -0
- data/lib/smart_core/types/value/time_based.rb +8 -5
- data/lib/smart_core/types/value/unbound_method.rb +9 -0
- data/lib/smart_core/types/value.rb +9 -0
- data/lib/smart_core/types/variadic/array_of.rb +23 -0
- data/lib/smart_core/types/variadic/enum.rb +11 -0
- data/lib/smart_core/types/variadic/tuple.rb +23 -0
- data/lib/smart_core/types/variadic.rb +10 -0
- data/lib/smart_core/types/version.rb +2 -1
- data/lib/smart_core/types.rb +2 -0
- data/smart_types.gemspec +5 -4
- metadata +61 -18
- data/.travis.yml +0 -21
- 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
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -2,10 +2,12 @@
|
|
2
2
|
|
3
3
|
# @api public
|
4
4
|
# @since 0.1.0
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
)
|
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,11 +2,14 @@
|
|
2
2
|
|
3
3
|
# @api public
|
4
4
|
# @since 0.1.0
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
)
|
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)
|
@@ -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
|