smart_types 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +15 -0
  5. data/.travis.yml +20 -0
  6. data/CHANGELOG.md +2 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +5 -0
  9. data/Gemfile.lock +77 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +85 -0
  12. data/Rakefile +21 -0
  13. data/bin/console +8 -0
  14. data/bin/setup +8 -0
  15. data/lib/smart_core/types.rb +13 -0
  16. data/lib/smart_core/types/errors.rb +27 -0
  17. data/lib/smart_core/types/primitive.rb +76 -0
  18. data/lib/smart_core/types/primitive/caster.rb +31 -0
  19. data/lib/smart_core/types/primitive/checker.rb +31 -0
  20. data/lib/smart_core/types/primitive/factory.rb +96 -0
  21. data/lib/smart_core/types/primitive/factory/definition_context.rb +46 -0
  22. data/lib/smart_core/types/primitive/mult_checker.rb +31 -0
  23. data/lib/smart_core/types/primitive/mult_factory.rb +69 -0
  24. data/lib/smart_core/types/primitive/mult_factory/definition_context.rb +29 -0
  25. data/lib/smart_core/types/primitive/sum_checker.rb +31 -0
  26. data/lib/smart_core/types/primitive/sum_factory.rb +69 -0
  27. data/lib/smart_core/types/primitive/sum_factory/definition_context.rb +29 -0
  28. data/lib/smart_core/types/primitive/undefined_caster.rb +19 -0
  29. data/lib/smart_core/types/system.rb +7 -0
  30. data/lib/smart_core/types/system/producer_dsl.rb +30 -0
  31. data/lib/smart_core/types/value.rb +19 -0
  32. data/lib/smart_core/types/value/any.rb +14 -0
  33. data/lib/smart_core/types/value/array.rb +17 -0
  34. data/lib/smart_core/types/value/boolean.rb +9 -0
  35. data/lib/smart_core/types/value/class.rb +9 -0
  36. data/lib/smart_core/types/value/float.rb +17 -0
  37. data/lib/smart_core/types/value/hash.rb +17 -0
  38. data/lib/smart_core/types/value/integer.rb +17 -0
  39. data/lib/smart_core/types/value/module.rb +9 -0
  40. data/lib/smart_core/types/value/numeric.rb +17 -0
  41. data/lib/smart_core/types/value/proc.rb +11 -0
  42. data/lib/smart_core/types/value/string.rb +11 -0
  43. data/lib/smart_core/types/value/symbol.rb +11 -0
  44. data/lib/smart_core/types/value/text.rb +11 -0
  45. data/lib/smart_core/types/version.rb +11 -0
  46. data/smart_types.gemspec +42 -0
  47. metadata +178 -0
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::Caster
6
+ # @param expression [Proc]
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def initialize(expression)
12
+ @expression = expression
13
+ end
14
+
15
+ # @param value [Any]
16
+ # @return [Any]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ def call(value)
21
+ expression.call(value)
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Proc]
27
+ #
28
+ # @api private
29
+ # @since 0.1.0
30
+ attr_reader :expression
31
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::Checker
6
+ # @param expression [Proc]
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def initialize(expression)
12
+ @expression = expression
13
+ end
14
+
15
+ # @param value [Any]
16
+ # @return [Boolean]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ def call(value)
21
+ !!expression.call(value)
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Proc]
27
+ #
28
+ # @api private
29
+ # @since 0.1.0
30
+ attr_reader :expression
31
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::Factory
6
+ require_relative 'factory/definition_context'
7
+
8
+ class << self
9
+ # @param type_category [Class<SmartCore::Types::Primitive>]
10
+ # @param type_name [String, Symbol]
11
+ # @param type_definition [Proc]
12
+ # @return [SmartCore::Types::Primitive]
13
+ #
14
+ # @api private
15
+ # @since 0.1.0
16
+ def create_type(type_category, type_name, type_definition)
17
+ type_definitions = build_type_definitions(type_definition)
18
+ type_checker = build_type_checker(type_definitions)
19
+ type_caster = build_type_caster(type_definitions)
20
+ type = build_type(type_category, type_checker, type_caster)
21
+ type.tap { register_new_type(type_category, type_name, type) }
22
+ end
23
+
24
+ private
25
+
26
+ # @param type_definition [Proc]
27
+ # @return [SmartCore::Types::Primitive::Factory::DefinitionContext]
28
+ #
29
+ # @raise [SmartCore::Types::NoCheckerDefinitionError]
30
+ #
31
+ # @api private
32
+ # @since 0.1.0
33
+ def build_type_definitions(type_definition)
34
+ raise 'Type definition is not provied' unless type_definition.is_a?(Proc)
35
+ SmartCore::Types::Primitive::Factory::DefinitionContext.new.tap do |context|
36
+ context.instance_eval(&type_definition)
37
+ end.tap do |context|
38
+ raise(
39
+ SmartCore::Types::NoCheckerDefinitionError,
40
+ 'Type checker is not provided (use .define_checker for it)'
41
+ ) if context.type_checker.nil?
42
+ end
43
+ end
44
+
45
+ # @param type_definitions [SmartCore::Types::Primitive::Factory::DefinitionContext]
46
+ # @return [SmartCore::Types::Primitive::Checker]
47
+ #
48
+ # @api private
49
+ # @since 0.1.0
50
+ def build_type_checker(type_definitions)
51
+ SmartCore::Types::Primitive::Checker.new(type_definitions.type_checker)
52
+ end
53
+
54
+ # @param type_definitions [SmartCore::Types::Primitive::Factory::DefinitionContext]
55
+ # @return [SmartCore::Types::Primitive::Caster]
56
+ #
57
+ # @api private
58
+ # @since 0.1.0
59
+ def build_type_caster(type_definitions)
60
+ if type_definitions.type_caster.nil?
61
+ SmartCore::Types::Primitive::UndefinedCaster.new
62
+ else
63
+ SmartCore::Types::Primitive::Caster.new(type_definitions.type_caster)
64
+ end
65
+ end
66
+
67
+ # @param type_klass [Class<SmartCore::Types::Primitive>]
68
+ # @param type_checker [SmartCore::Types::Primitive::Checker]
69
+ # @param type_caster [SmartCore::Types::Primitive::Caster]
70
+ # @return [SmartCore::Types::Primitive]
71
+ #
72
+ # @api private
73
+ # @since 0.1.0
74
+ def build_type(type_category, type_checker, type_caster)
75
+ Class.new(type_category).new(type_checker, type_caster)
76
+ end
77
+
78
+ # @param type_category [Class<SmartCore::Types::Primitive>]
79
+ # @param type_name [String, Symbol]
80
+ # @param type [SmartCore::Types::Primitive]
81
+ # @return [void]
82
+ #
83
+ # @raise [SmartCore::Types::IncorrectTypeNameError]
84
+ #
85
+ # @api private
86
+ # @since 0.1.0
87
+ def register_new_type(type_category, type_name, type)
88
+ type_category.const_set(type_name, type)
89
+ rescue ::NameError
90
+ raise(
91
+ SmartCore::Types::IncorrectTypeNameError,
92
+ "Incorrect constant name for new type (#{type_name})"
93
+ )
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::Factory::DefinitionContext
6
+ # @return [Proc]
7
+ #
8
+ # @api private
9
+ # @since 0.1.0
10
+ attr_reader :type_checker
11
+
12
+ # @return [Proc]
13
+ #
14
+ # @api private
15
+ # @since 0.1.0
16
+ attr_reader :type_caster
17
+
18
+ # @return [void]
19
+ #
20
+ # @api private
21
+ # @since 0.1.0
22
+ def initialize
23
+ @type_checker = nil
24
+ @type_caster = nil
25
+ end
26
+
27
+ # @param checker [Block]
28
+ # @return [void]
29
+ #
30
+ # @api public
31
+ # @since 0.1.0
32
+ def define_checker(&checker)
33
+ raise 'No checker definition block' unless block_given?
34
+ @type_checker = checker
35
+ end
36
+
37
+ # @param caster [Block]
38
+ # @return [void]
39
+ #
40
+ # @api public
41
+ # @since 0.1.0
42
+ def define_caster(&caster)
43
+ raise 'No caster definition block' unless block_given?
44
+ @type_caster = caster
45
+ end
46
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::MultChecker
6
+ # @param [Array<SmartCore::Types::Primitive::Checker>]
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def initialize(*checkers)
12
+ @checkers = checkers
13
+ end
14
+
15
+ # @param value [Any]
16
+ # @return [Boolean]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ def call(value)
21
+ checkers.all? { |checker| checker.call(value) }
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Array<SmartCore::Types::Primitive::Checker>]
27
+ #
28
+ # @api private
29
+ # @since 0.1.0
30
+ attr_reader :checkers
31
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ module SmartCore::Types::Primitive::MultFactory
6
+ require_relative 'mult_factory/definition_context'
7
+
8
+ class << self
9
+ # @param types [Array<SmartCore::Types::Primtive>]
10
+ # @param type_definition [NilClass, Proc]
11
+ # @return [SmartCore::Types::Primitive]
12
+ #
13
+ # @api private
14
+ # @since 0.1.0
15
+ def create_type(types, type_definition)
16
+ type_definitions = build_type_definitions(type_definition)
17
+ type_checker = build_type_checker(types, type_definitions)
18
+ type_caster = build_type_caster(types, type_definitions)
19
+ build_type(type_checker, type_caster)
20
+ end
21
+
22
+ private
23
+
24
+ # @param type_definition [NilClass, Proc]
25
+ # @return [SmartCore::Types::Primitive::MultFactory::DefinitionContext]
26
+ #
27
+ # @pai private
28
+ # @since 0.1.0
29
+ def build_type_definitions(type_definition)
30
+ SmatCore::Types::Primitive::MultFactory::DefinitionContext.new.tap do |context|
31
+ context.instance_eval(&type_definition) if type_definition
32
+ end
33
+ end
34
+
35
+ # @param types [Array<SmartCore::Types::Primtive>]
36
+ # @param type_definitions [SmartCore::Types::Primitive::MultFactory::DefinitionContext]
37
+ # @return [SmartCore::Types::Primitive::MultChecker]
38
+ #
39
+ # @api private
40
+ # @since 0.1.0
41
+ def build_type_checker(types, type_definitions)
42
+ SmartCore::Types::Primitive::MultChecker.new(*types.map(&:checker))
43
+ end
44
+
45
+ # @param types [Array<SmartCore::Types::Primtive>]
46
+ # @param type_definition [SmartCore::Types::Primitive::MultFactory::DefinitionContext]
47
+ # @return [SmartCore::Types::Primitive::Caster]
48
+ #
49
+ # @api private
50
+ # @since 0.1.0
51
+ def build_type_caster(types, type_definitions)
52
+ if type_definitions.type_caster.nil?
53
+ SmartCore::Types::Primitive::UndefinedCaster.new
54
+ else
55
+ SmartCore::Types::Primitive::Caster.new(type_definitions.type_caster)
56
+ end
57
+ end
58
+
59
+ # @param type_checker [SmartCore::Types::Primitive::MultChecker]
60
+ # @param type_caster [SmartCore::Types::Primitive::Caster]
61
+ # @return [SmartCore::Types::Primitive]
62
+ #
63
+ # @api private
64
+ # @since 0.1.0
65
+ def build_type(type_checker, type_caster)
66
+ SmartCore::Types::Primitive.new(type_checker, type_caster)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::Factory::DefinitionContext
6
+ # @return [Proc]
7
+ #
8
+ # @api private
9
+ # @since 0.1.0
10
+ attr_reader :type_caster
11
+
12
+ # @return [void]
13
+ #
14
+ # @api private
15
+ # @since 0.1.0
16
+ def initialize
17
+ @type_caster = nil
18
+ end
19
+
20
+ # @param caster [Block]
21
+ # @return [void]
22
+ #
23
+ # @api public
24
+ # @since 0.1.0
25
+ def define_caster(&caster)
26
+ raise 'No caster definition block' unless block_given?
27
+ @type_caster = caster
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::SumChecker
6
+ # @param [Array<SmartCore::Types::Primitive::Checker>]
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def initialize(*checkers)
12
+ @checkers = checkers
13
+ end
14
+
15
+ # @param value [Any]
16
+ # @return [Boolean]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ def call(value)
21
+ checkers.any? { |checker| checker.call(value) }
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Array<SmartCore::Types::Primitive::Checker>]
27
+ #
28
+ # @api private
29
+ # @since 0.1.0
30
+ attr_reader :checkers
31
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ module SmartCore::Types::Primitive::SumFactory
6
+ require_relative 'sum_factory/definition_context'
7
+
8
+ class << self
9
+ # @param types [Array<SmartCore::Types::Primtive>]
10
+ # @param type_definition [NilClass, Proc]
11
+ # @return [SmartCore::Types::Primitive]
12
+ #
13
+ # @api private
14
+ # @since 0.1.0
15
+ def create_type(types, type_definition)
16
+ type_definitions = build_type_definitions(type_definition)
17
+ type_checker = build_type_checker(types, type_definitions)
18
+ type_caster = build_type_caster(types, type_definitions)
19
+ build_type(type_checker, type_caster)
20
+ end
21
+
22
+ private
23
+
24
+ # @param type_definition [NilClass, Proc]
25
+ # @return [SmartCore::Types::Primitive::SumFactory::DefinitionContext]
26
+ #
27
+ # @pai private
28
+ # @since 0.1.0
29
+ def build_type_definitions(type_definition)
30
+ SmatCore::Types::Primitive::SumFactory::DefinitionContext.new.tap do |context|
31
+ context.instance_eval(&type_definition) if type_definition
32
+ end
33
+ end
34
+
35
+ # @param types [Array<SmartCore::Types::Primtive>]
36
+ # @param type_definitions [SmartCore::Types::Primitive::SumFactory::DefinitionContext]
37
+ # @return [SmartCore::Types::Primitive::SumChecker]
38
+ #
39
+ # @api private
40
+ # @since 0.1.0
41
+ def build_type_checker(types, type_definitions)
42
+ SmartCore::Types::Primitive::SumChecker.new(*types.map(&:checker))
43
+ end
44
+
45
+ # @param types [Array<SmartCore::Types::Primtive>]
46
+ # @param type_definition [SmartCore::Types::Primitive::SumFactory::DefinitionContext]
47
+ # @return [SmartCore::Types::Primitive::Caster]
48
+ #
49
+ # @api private
50
+ # @since 0.1.0
51
+ def build_type_caster(types, type_definitions)
52
+ if type_definitions.type_caster.nil?
53
+ SmartCore::Types::Primitive::UndefinedCaster.new
54
+ else
55
+ SmartCore::Types::Primitive::Caster.new(type_definitions.type_caster)
56
+ end
57
+ end
58
+
59
+ # @param type_checker [SmartCore::Types::Primitive::SumChecker]
60
+ # @param type_caster [SmartCore::Types::Primitive::Caster]
61
+ # @return [SmartCore::Types::Primitive]
62
+ #
63
+ # @api private
64
+ # @since 0.1.0
65
+ def build_type(type_checker, type_caster)
66
+ SmartCore::Types::Primitive.new(type_checker, type_caster)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Types::Primitive::SumFactory::DefinitionContext
6
+ # @return [Proc]
7
+ #
8
+ # @api private
9
+ # @since 0.1.0
10
+ attr_reader :type_caster
11
+
12
+ # @return [void]
13
+ #
14
+ # @api private
15
+ # @since 0.1.0
16
+ def initialize
17
+ @type_caster = nil
18
+ end
19
+
20
+ # @param caster [Block]
21
+ # @return [void]
22
+ #
23
+ # @api public
24
+ # @since 0.1.0
25
+ def define_caster(&caster)
26
+ raise 'No caster definition block' unless block_given?
27
+ @type_caster = caster
28
+ end
29
+ end