kind 5.1.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.tool-versions +1 -1
  3. data/.travis.sh +35 -10
  4. data/CHANGELOG.md +364 -26
  5. data/README.md +37 -36
  6. data/lib/kind.rb +2 -49
  7. data/lib/kind/__lib__/attributes.rb +66 -0
  8. data/lib/kind/__lib__/kind.rb +71 -0
  9. data/lib/kind/__lib__/undefined.rb +14 -0
  10. data/lib/kind/action.rb +92 -0
  11. data/lib/kind/active_model/validation.rb +1 -1
  12. data/lib/kind/basic.rb +73 -0
  13. data/lib/kind/basic/error.rb +29 -0
  14. data/lib/kind/{core → basic}/undefined.rb +6 -1
  15. data/lib/kind/{core/dig.rb → dig.rb} +20 -4
  16. data/lib/kind/either.rb +30 -0
  17. data/lib/kind/either/left.rb +29 -0
  18. data/lib/kind/either/methods.rb +17 -0
  19. data/lib/kind/either/monad.rb +65 -0
  20. data/lib/kind/either/monad/wrapper.rb +19 -0
  21. data/lib/kind/either/right.rb +38 -0
  22. data/lib/kind/{core/empty.rb → empty.rb} +2 -0
  23. data/lib/kind/{core/empty → empty}/constant.rb +0 -0
  24. data/lib/kind/enum.rb +63 -0
  25. data/lib/kind/enum/item.rb +40 -0
  26. data/lib/kind/enum/methods.rb +72 -0
  27. data/lib/kind/function.rb +47 -0
  28. data/lib/kind/functional.rb +89 -0
  29. data/lib/kind/functional/action.rb +89 -0
  30. data/lib/kind/immutable_attributes.rb +34 -0
  31. data/lib/kind/immutable_attributes/initializer.rb +70 -0
  32. data/lib/kind/immutable_attributes/reader.rb +38 -0
  33. data/lib/kind/maybe.rb +69 -0
  34. data/lib/kind/maybe/methods.rb +21 -0
  35. data/lib/kind/maybe/monad.rb +82 -0
  36. data/lib/kind/maybe/monad/wrapper.rb +19 -0
  37. data/lib/kind/{core/maybe → maybe}/none.rb +11 -18
  38. data/lib/kind/maybe/some.rb +132 -0
  39. data/lib/kind/maybe/typed.rb +35 -0
  40. data/lib/kind/{core/maybe/wrappable.rb → maybe/wrapper.rb} +8 -4
  41. data/lib/kind/monad.rb +22 -0
  42. data/lib/kind/monads.rb +5 -0
  43. data/lib/kind/objects.rb +17 -0
  44. data/lib/kind/objects/basic_object.rb +45 -0
  45. data/lib/kind/objects/modules.rb +32 -0
  46. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/array.rb +3 -1
  47. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/class.rb +1 -1
  48. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/comparable.rb +1 -1
  49. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerable.rb +1 -1
  50. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerator.rb +1 -1
  51. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/file.rb +1 -1
  52. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/float.rb +1 -1
  53. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/hash.rb +3 -1
  54. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/integer.rb +1 -1
  55. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/io.rb +1 -1
  56. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/method.rb +1 -1
  57. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/module.rb +1 -1
  58. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/numeric.rb +1 -1
  59. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/proc.rb +1 -1
  60. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/queue.rb +1 -1
  61. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/range.rb +1 -1
  62. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/regexp.rb +1 -1
  63. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/string.rb +3 -1
  64. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/struct.rb +1 -1
  65. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/symbol.rb +1 -1
  66. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/time.rb +1 -1
  67. data/lib/kind/{core/type_checkers → objects/modules}/custom/boolean.rb +2 -2
  68. data/lib/kind/{core/type_checkers → objects/modules}/custom/callable.rb +1 -1
  69. data/lib/kind/{core/type_checkers → objects/modules}/custom/lambda.rb +1 -1
  70. data/lib/kind/{core/type_checkers → objects/modules}/stdlib/open_struct.rb +3 -1
  71. data/lib/kind/{core/type_checkers → objects/modules}/stdlib/set.rb +3 -1
  72. data/lib/kind/objects/nil.rb +17 -0
  73. data/lib/kind/objects/not_nil.rb +13 -0
  74. data/lib/kind/objects/object.rb +56 -0
  75. data/lib/kind/objects/respond_to.rb +30 -0
  76. data/lib/kind/objects/union_type.rb +44 -0
  77. data/lib/kind/{core/presence.rb → presence.rb} +4 -2
  78. data/lib/kind/result.rb +31 -0
  79. data/lib/kind/result/abstract.rb +53 -0
  80. data/lib/kind/result/failure.rb +31 -0
  81. data/lib/kind/result/methods.rb +17 -0
  82. data/lib/kind/result/monad.rb +69 -0
  83. data/lib/kind/result/monad/wrapper.rb +19 -0
  84. data/lib/kind/result/success.rb +40 -0
  85. data/lib/kind/try.rb +46 -0
  86. data/lib/kind/validator.rb +14 -10
  87. data/lib/kind/version.rb +1 -1
  88. metadata +81 -47
  89. data/lib/kind/core.rb +0 -15
  90. data/lib/kind/core/error.rb +0 -15
  91. data/lib/kind/core/maybe.rb +0 -42
  92. data/lib/kind/core/maybe/result.rb +0 -51
  93. data/lib/kind/core/maybe/some.rb +0 -90
  94. data/lib/kind/core/maybe/typed.rb +0 -29
  95. data/lib/kind/core/try.rb +0 -34
  96. data/lib/kind/core/type_checker.rb +0 -87
  97. data/lib/kind/core/type_checkers.rb +0 -30
  98. data/lib/kind/core/utils/kind.rb +0 -61
  99. data/lib/kind/core/utils/undefined.rb +0 -7
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kind
4
+ module Enum
5
+ class Item
6
+ Underscore = ->(arg) do
7
+ str = String(arg).strip
8
+ str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
9
+ str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
10
+ str.tr!("-", "_")
11
+ str.downcase!
12
+ str
13
+ end
14
+
15
+ attr_reader :value, :to_s, :name, :to_sym, :inspect
16
+
17
+ alias_method :key, :to_s
18
+ alias_method :to_str, :to_s
19
+
20
+ def initialize(key, val)
21
+ @value = val.frozen? ? val : val.dup.freeze
22
+
23
+ @to_s = Kind.respond_to(key, :to_sym).to_s
24
+ @name = Underscore[key].upcase.freeze
25
+ @to_sym = key.to_sym
26
+ @inspect = ('#<Kind::Enum::Item name=%p to_s=%p value=%p>' % [@name, @to_s, @value]).freeze
27
+ end
28
+
29
+ def ==(arg)
30
+ arg == value || arg == to_s || arg == to_sym
31
+ end
32
+
33
+ def to_ary
34
+ [key, value]
35
+ end
36
+
37
+ alias_method :===, :==
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kind
4
+ module Enum
5
+ METHODS = \
6
+ <<-RUBY
7
+ def to_h
8
+ ENUM__HASH
9
+ end
10
+
11
+ def items
12
+ ENUM__ITEMS
13
+ end
14
+
15
+ def ===(arg)
16
+ ENUM__ITEMS.any? { |item| item === arg }
17
+ end
18
+
19
+ def refs
20
+ ENUM__REFS.to_a
21
+ end
22
+
23
+ def keys
24
+ ENUM__KEYS.to_a
25
+ end
26
+
27
+ def values
28
+ ENUM__VALS.to_a
29
+ end
30
+
31
+ def ref?(arg)
32
+ ENUM__REFS.include?(arg)
33
+ end
34
+
35
+ def key?(arg)
36
+ arg.respond_to?(:to_sym) ? ref?(arg) && !value?(arg) : false
37
+ end
38
+
39
+ def value?(arg)
40
+ ENUM__VALS.include?(arg)
41
+ end
42
+
43
+ def [](arg)
44
+ return arg if ref?(arg)
45
+
46
+ raise KeyError, "key or value not found: %p" % [arg]
47
+ end
48
+
49
+ def ref(arg)
50
+ arg if ref?(arg)
51
+ end
52
+
53
+ def item(arg)
54
+ ENUM__MAP[arg]
55
+ end
56
+
57
+ def key(arg)
58
+ item(arg).key if value?(arg)
59
+ end
60
+
61
+ def value_at(arg)
62
+ item(arg).value if key?(arg)
63
+ end
64
+
65
+ def self.included(base)
66
+ base.extend(base)
67
+ end
68
+ RUBY
69
+
70
+ private_constant :METHODS
71
+ end
72
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/basic'
4
+
5
+ module Kind
6
+ module Function
7
+ module Behavior
8
+ def self.extended(base)
9
+ base.send(:alias_method, :[], :call)
10
+ base.send(:alias_method, :===, :call)
11
+ base.send(:alias_method, :yield, :call)
12
+ end
13
+ end
14
+
15
+ def self.included(_)
16
+ raise RuntimeError, "The Kind::Function can't be included, it can be only extended."
17
+ end
18
+
19
+ def self.extended(base)
20
+ KIND.error!('Module', base) unless Kind.of_module?(base)
21
+
22
+ base.extend(base)
23
+ end
24
+
25
+ def kind_function!
26
+ return self if Kind.is?(Behavior, self)
27
+
28
+ KIND.respond_to!(:call, self).extend(Behavior)
29
+
30
+ if method(:call).parameters.empty?
31
+ raise ArgumentError, "#{self.name}.call must receive at least one argument"
32
+ end
33
+
34
+ self.instance_eval(
35
+ 'def to_proc; @to_proc ||= method(:call).to_proc; end' \
36
+ "\n" \
37
+ 'def curry; @curry ||= to_proc.curry; end'
38
+ )
39
+
40
+ self.to_proc
41
+ self.curry
42
+ self
43
+ end
44
+
45
+ private_constant :Behavior
46
+ end
47
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/basic'
4
+ require 'kind/empty'
5
+ require 'kind/__lib__/attributes'
6
+
7
+ module Kind
8
+ module Functional
9
+ def self.extended(_)
10
+ raise RuntimeError, "The Kind::Functional can't be extended, it can be only included."
11
+ end
12
+
13
+ def self.included(base)
14
+ KIND.of!(::Class, base).send(:extend, ClassMethods)
15
+ end
16
+
17
+ module Behavior
18
+ def self.included(base)
19
+ base.send(:alias_method, :[], :call)
20
+ base.send(:alias_method, :===, :call)
21
+ base.send(:alias_method, :yield, :call)
22
+ end
23
+
24
+ def initialize(arg = Empty::HASH)
25
+ hash = KIND.of!(::Hash, arg)
26
+
27
+ self.class.__dependencies__.each do |name, (kind, default, _visibility)|
28
+ value_to_assign = ATTRIBUTES.value_to_assign!(kind, default, hash, name)
29
+
30
+ instance_variable_set("@#{name}", value_to_assign)
31
+ end
32
+ end
33
+ end
34
+
35
+ module DependencyInjection
36
+ def __dependencies__ # :nodoc:
37
+ @__dependencies__ ||= {}
38
+ end
39
+
40
+ def dependency(name, kind, default: UNDEFINED)
41
+ __dependencies__[ATTRIBUTES.name!(name)] = ATTRIBUTES.value!(kind, default)
42
+
43
+ attr_reader(name)
44
+
45
+ private(name)
46
+
47
+ name
48
+ end
49
+ end
50
+
51
+ module ClassMethods
52
+ include DependencyInjection
53
+
54
+ def kind_functional!
55
+ return self if Kind.is?(Behavior, self)
56
+
57
+ public_methods = self.public_instance_methods - ::Object.new.methods
58
+
59
+ unless public_methods.include?(:call)
60
+ raise Kind::Error.new("expected #{self} to implement `#call`")
61
+ end
62
+
63
+ if public_methods.size > 1
64
+ raise Kind::Error.new("#{self} can only have `#call` as its public method")
65
+ end
66
+
67
+ if public_instance_method(:call).parameters.empty?
68
+ raise ArgumentError, "#{self.name}#call must receive at least one argument"
69
+ end
70
+
71
+ self.send(:include, Behavior)
72
+
73
+ def self.inherited(_)
74
+ raise RuntimeError, "#{self.name} is a Kind::Functional and it can't be inherited by anyone"
75
+ end
76
+
77
+ self.class_eval(
78
+ 'def to_proc; @to_proc ||= method(:call).to_proc; end' \
79
+ "\n" \
80
+ 'def curry; @curry ||= to_proc.curry; end'
81
+ )
82
+
83
+ __dependencies__.freeze
84
+
85
+ self
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/result'
4
+ require 'kind/functional'
5
+
6
+ module Kind
7
+ module Functional::Action
8
+ CALL_TMPL = [
9
+ 'def call(%s)',
10
+ ' result = call!(%s)',
11
+ '',
12
+ ' return result if Kind::Result::Monad === result',
13
+ '',
14
+ ' raise Kind::Error, "#{self.class.name}#call! must return a Kind::Success or Kind::Failure"',
15
+ 'end'
16
+ ].join("\n").freeze
17
+
18
+ module Macros
19
+ def kind_functional_action!
20
+ return self if Kind.is?(Result::Methods, self)
21
+
22
+ public_methods = self.public_instance_methods - ::Object.new.methods
23
+
24
+ unless public_methods.include?(:call!)
25
+ raise Kind::Error.new("expected #{self} to implement `#call!`")
26
+ end
27
+
28
+ if public_methods.size > 1
29
+ raise Kind::Error.new("#{self} can only have `#call!` as its public method")
30
+ end
31
+
32
+ call_parameters = public_instance_method(:call!).parameters
33
+
34
+ if call_parameters.empty?
35
+ raise ArgumentError, "#{self.name}#call! must receive at least one argument"
36
+ end
37
+
38
+ def self.inherited(_)
39
+ raise RuntimeError, "#{self.name} is a Kind::Functional::Action and it can't be inherited"
40
+ end
41
+
42
+ call_parameters.flatten!
43
+
44
+ call_with_args = call_parameters.include?(:req) || call_parameters.include?(:rest)
45
+ call_with_kargs = call_parameters.include?(:keyreq) || call_parameters.include?(:keyrest)
46
+
47
+ call_tmpl_args = '*args, **kargs' if call_with_args && call_with_kargs
48
+ call_tmpl_args = '*args' if call_with_args && !call_with_kargs
49
+ call_tmpl_args = '**kargs' if !call_with_args && call_with_kargs
50
+
51
+ self.class_eval(
52
+ "def to_proc; @to_proc ||= method(:call!).to_proc; end" \
53
+ "\n" \
54
+ "def curry; @curry ||= to_proc.curry; end" \
55
+ "\n" \
56
+ "#{CALL_TMPL % [call_tmpl_args, call_tmpl_args]}"
57
+ )
58
+
59
+ if KIND.of_module?(self)
60
+ self.send(:extend, Result::Methods)
61
+ else
62
+ self.send(:include, Result::Methods)
63
+ self.send(:include, Functional::Behavior)
64
+
65
+ __dependencies__.freeze
66
+ end
67
+
68
+ self.send(:protected, :call!)
69
+
70
+ self
71
+ end
72
+ end
73
+
74
+ def self.included(base)
75
+ KIND.of!(::Class, base).send(:extend, Functional::DependencyInjection)
76
+
77
+ base.send(:extend, Macros)
78
+ end
79
+
80
+ def self.extended(base)
81
+ KIND.error!('Module', base) unless Kind.of_module?(base)
82
+
83
+ base.send(:extend, base)
84
+ base.send(:extend, Macros)
85
+ end
86
+
87
+ private_constant :Macros, :CALL_TMPL
88
+ end
89
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/basic'
4
+ require 'kind/__lib__/attributes'
5
+
6
+ module Kind
7
+ module ImmutableAttributes
8
+ require 'kind/immutable_attributes/initializer'
9
+ require 'kind/immutable_attributes/reader'
10
+
11
+ module ClassMethods
12
+ def __attributes__ # :nodoc:
13
+ @__attributes__ ||= {}
14
+ end
15
+
16
+ def attribute(name, kind = nil, default: UNDEFINED, visibility: :public)
17
+ __attributes__[ATTRIBUTES.name!(name)] = ATTRIBUTES.value(kind, default, visibility)
18
+
19
+ attr_reader(name)
20
+
21
+ private(name) if visibility == :private
22
+ protected(name) if visibility == :protected
23
+
24
+ name
25
+ end
26
+ end
27
+
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ base.send(:include, Reader)
31
+ base.send(:include, Initializer)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/__lib__/attributes'
4
+ require 'kind/basic'
5
+ require 'kind/empty'
6
+
7
+ module Kind
8
+ module ImmutableAttributes
9
+ module Initializer
10
+ def initialize(arg)
11
+ input = __resolve_attribute_input(arg)
12
+
13
+ hash = call_before_initialize_to_prepare_the_input(input)
14
+
15
+ @_nil_attrs = ::Set.new
16
+ @_____attrs = {}
17
+ @attributes = {}
18
+
19
+ self.class.__attributes__.each do |name, (kind, default, visibility)|
20
+ value_to_assign = __resolve_attribute_value_to_assign(kind, default, hash, name)
21
+
22
+ @_nil_attrs << name if value_to_assign.nil?
23
+ @_____attrs[name] = value_to_assign
24
+ @attributes[name] = value_to_assign if visibility == :public
25
+
26
+ instance_variable_set("@#{name}", value_to_assign)
27
+ end
28
+
29
+ @_nil_attrs.freeze
30
+ @attributes.freeze
31
+
32
+ call_after_initialize_and_assign_the_attributes
33
+ end
34
+
35
+ def nil_attributes
36
+ @_nil_attrs.to_a
37
+ end
38
+
39
+ def nil_attributes?(*names)
40
+ names.empty? ? !@_nil_attrs.empty? : names.all? { |name| @_nil_attrs.include?(name) }
41
+ end
42
+
43
+ private
44
+
45
+ def call_before_initialize_to_prepare_the_input(input)
46
+ input
47
+ end
48
+
49
+ def call_after_initialize_and_assign_the_attributes
50
+ end
51
+
52
+ def __resolve_attribute_input(arg)
53
+ return arg.attributes if arg.kind_of?(ImmutableAttributes)
54
+
55
+ arg.kind_of?(::Hash) ? arg : Empty::HASH
56
+ end
57
+
58
+ def __resolve_attribute_value_to_assign(kind, default, hash, name)
59
+ if kind.kind_of?(::Class) && kind < ImmutableAttributes
60
+ kind.new(hash[name])
61
+ elsif kind.kind_of?(::Array) && (nkind = kind[0]).kind_of?(::Class) && nkind < ImmutableAttributes
62
+ Array(hash[name]).map { |item| nkind.new(item) }
63
+ else
64
+ ATTRIBUTES.value_to_assign(kind, default, hash, name)
65
+ end
66
+ end
67
+ end
68
+
69
+ end
70
+ end