kind 5.1.0 → 5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.tool-versions +1 -1
  3. data/.travis.sh +37 -10
  4. data/CHANGELOG.md +518 -29
  5. data/README.md +48 -45
  6. data/kind.gemspec +2 -2
  7. data/lib/kind.rb +2 -49
  8. data/lib/kind/__lib__/action_steps.rb +57 -0
  9. data/lib/kind/__lib__/attributes.rb +66 -0
  10. data/lib/kind/__lib__/kind.rb +51 -0
  11. data/lib/kind/__lib__/of.rb +17 -0
  12. data/lib/kind/__lib__/strict.rb +49 -0
  13. data/lib/kind/__lib__/undefined.rb +14 -0
  14. data/lib/kind/action.rb +127 -0
  15. data/lib/kind/active_model/validation.rb +1 -1
  16. data/lib/kind/basic.rb +83 -0
  17. data/lib/kind/basic/error.rb +29 -0
  18. data/lib/kind/{core → basic}/undefined.rb +6 -1
  19. data/lib/kind/{core/dig.rb → dig.rb} +21 -5
  20. data/lib/kind/either.rb +30 -0
  21. data/lib/kind/either/left.rb +29 -0
  22. data/lib/kind/either/methods.rb +17 -0
  23. data/lib/kind/either/monad.rb +65 -0
  24. data/lib/kind/either/monad/wrapper.rb +19 -0
  25. data/lib/kind/either/right.rb +38 -0
  26. data/lib/kind/{core/empty.rb → empty.rb} +2 -0
  27. data/lib/kind/{core/empty → empty}/constant.rb +0 -0
  28. data/lib/kind/enum.rb +63 -0
  29. data/lib/kind/enum/item.rb +40 -0
  30. data/lib/kind/enum/methods.rb +72 -0
  31. data/lib/kind/function.rb +45 -0
  32. data/lib/kind/functional.rb +89 -0
  33. data/lib/kind/functional/action.rb +87 -0
  34. data/lib/kind/functional/steps.rb +22 -0
  35. data/lib/kind/immutable_attributes.rb +34 -0
  36. data/lib/kind/immutable_attributes/initializer.rb +70 -0
  37. data/lib/kind/immutable_attributes/reader.rb +38 -0
  38. data/lib/kind/maybe.rb +69 -0
  39. data/lib/kind/maybe/methods.rb +21 -0
  40. data/lib/kind/maybe/monad.rb +82 -0
  41. data/lib/kind/maybe/monad/wrapper.rb +19 -0
  42. data/lib/kind/maybe/none.rb +50 -0
  43. data/lib/kind/maybe/some.rb +132 -0
  44. data/lib/kind/maybe/typed.rb +35 -0
  45. data/lib/kind/{core/maybe/wrappable.rb → maybe/wrapper.rb} +8 -4
  46. data/lib/kind/monad.rb +22 -0
  47. data/lib/kind/monads.rb +5 -0
  48. data/lib/kind/objects.rb +17 -0
  49. data/lib/kind/objects/basic_object.rb +43 -0
  50. data/lib/kind/objects/modules.rb +32 -0
  51. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/array.rb +3 -1
  52. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/class.rb +1 -1
  53. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/comparable.rb +1 -1
  54. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerable.rb +1 -1
  55. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerator.rb +1 -1
  56. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/file.rb +1 -1
  57. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/float.rb +1 -1
  58. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/hash.rb +3 -1
  59. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/integer.rb +1 -1
  60. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/io.rb +1 -1
  61. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/method.rb +1 -1
  62. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/module.rb +1 -1
  63. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/numeric.rb +1 -1
  64. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/proc.rb +1 -1
  65. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/queue.rb +1 -1
  66. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/range.rb +1 -1
  67. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/regexp.rb +1 -1
  68. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/string.rb +3 -1
  69. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/struct.rb +1 -1
  70. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/symbol.rb +1 -1
  71. data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/time.rb +1 -1
  72. data/lib/kind/{core/type_checkers → objects/modules}/custom/boolean.rb +2 -2
  73. data/lib/kind/{core/type_checkers → objects/modules}/custom/callable.rb +1 -1
  74. data/lib/kind/{core/type_checkers → objects/modules}/custom/lambda.rb +1 -1
  75. data/lib/kind/{core/type_checkers → objects/modules}/stdlib/open_struct.rb +3 -1
  76. data/lib/kind/{core/type_checkers → objects/modules}/stdlib/set.rb +3 -1
  77. data/lib/kind/objects/nil.rb +17 -0
  78. data/lib/kind/objects/not_nil.rb +9 -0
  79. data/lib/kind/objects/object.rb +56 -0
  80. data/lib/kind/objects/respond_to.rb +30 -0
  81. data/lib/kind/objects/union_type.rb +44 -0
  82. data/lib/kind/{core/presence.rb → presence.rb} +4 -2
  83. data/lib/kind/result.rb +31 -0
  84. data/lib/kind/result/abstract.rb +53 -0
  85. data/lib/kind/result/failure.rb +33 -0
  86. data/lib/kind/result/methods.rb +17 -0
  87. data/lib/kind/result/monad.rb +74 -0
  88. data/lib/kind/result/monad/wrapper.rb +19 -0
  89. data/lib/kind/result/success.rb +53 -0
  90. data/lib/kind/strict/disabled.rb +34 -0
  91. data/lib/kind/try.rb +46 -0
  92. data/lib/kind/validator.rb +14 -10
  93. data/lib/kind/version.rb +1 -1
  94. metadata +91 -49
  95. data/lib/kind/core.rb +0 -15
  96. data/lib/kind/core/error.rb +0 -15
  97. data/lib/kind/core/maybe.rb +0 -42
  98. data/lib/kind/core/maybe/none.rb +0 -57
  99. data/lib/kind/core/maybe/result.rb +0 -51
  100. data/lib/kind/core/maybe/some.rb +0 -90
  101. data/lib/kind/core/maybe/typed.rb +0 -29
  102. data/lib/kind/core/try.rb +0 -34
  103. data/lib/kind/core/type_checker.rb +0 -87
  104. data/lib/kind/core/type_checkers.rb +0 -30
  105. data/lib/kind/core/utils/kind.rb +0 -61
  106. data/lib/kind/core/utils/undefined.rb +0 -7
data/lib/kind/core.rb DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'kind/core/utils/kind'
4
- require 'kind/core/utils/undefined'
5
-
6
- require 'kind/core/error'
7
- require 'kind/core/empty'
8
- require 'kind/core/dig'
9
- require 'kind/core/try'
10
- require 'kind/core/presence'
11
- require 'kind/core/undefined'
12
- require 'kind/core/maybe'
13
-
14
- require 'kind/core/type_checker'
15
- require 'kind/core/type_checkers'
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- class Error < TypeError
5
- def initialize(arg, object = UNDEFINED)
6
- if UNDEFINED == object
7
- # Will be used when the exception was raised with a message. e.g:
8
- # raise Kind::Error, "some message"
9
- super(arg)
10
- else
11
- super("#{object.inspect} expected to be a kind of #{arg}")
12
- end
13
- end
14
- end
15
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Maybe
5
- require 'kind/core/maybe/result'
6
- require 'kind/core/maybe/none'
7
- require 'kind/core/maybe/some'
8
- require 'kind/core/maybe/wrappable'
9
- require 'kind/core/maybe/typed'
10
-
11
- extend self
12
-
13
- def new(value)
14
- (KIND.null?(value) ? None : Some)
15
- .new(value)
16
- end
17
-
18
- alias_method :[], :new
19
-
20
- extend Wrappable
21
- end
22
-
23
- Optional = Maybe
24
-
25
- None = Maybe.none
26
-
27
- def self.None
28
- None
29
- end
30
-
31
- def self.Some(value)
32
- Maybe.some(value)
33
- end
34
-
35
- def self.Maybe(kind)
36
- Maybe::Typed.new(kind)
37
- end
38
-
39
- def self.Optional(kind)
40
- Maybe::Typed.new(kind)
41
- end
42
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Maybe
5
- class None < Result
6
- INVALID_DEFAULT_ARG = 'the default value must be defined as an argument or block'.freeze
7
-
8
- def value_or(default = UNDEFINED, &block)
9
- raise ArgumentError, INVALID_DEFAULT_ARG if UNDEFINED == default && !block
10
-
11
- UNDEFINED != default ? default : block.call
12
- end
13
-
14
- def none?; true; end
15
-
16
- def map(&fn)
17
- self
18
- end
19
-
20
- alias_method :map!, :map
21
- alias_method :then, :map
22
- alias_method :then!, :map
23
- alias_method :check, :map
24
-
25
- def try!(method_name = UNDEFINED, *args, &block)
26
- KIND.of!(::Symbol, method_name)if UNDEFINED != method_name
27
-
28
- self
29
- end
30
-
31
- alias_method :try, :try!
32
-
33
- def dig(*keys)
34
- self
35
- end
36
-
37
- def presence
38
- self
39
- end
40
-
41
- private_constant :INVALID_DEFAULT_ARG
42
- end
43
-
44
- NONE_WITH_NIL_VALUE = None.new(nil)
45
- NONE_WITH_UNDEFINED_VALUE = None.new(Undefined)
46
-
47
- def self.none
48
- NONE_WITH_NIL_VALUE
49
- end
50
-
51
- def self.__none__(value) # :nodoc:
52
- None.new(value)
53
- end
54
-
55
- private_constant :NONE_WITH_NIL_VALUE, :NONE_WITH_UNDEFINED_VALUE
56
- end
57
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Maybe
5
- class Result
6
- attr_reader :value
7
-
8
- Value = ->(arg) { arg.kind_of?(Maybe::Result) ? arg.value : arg } # :nodoc:
9
-
10
- def initialize(arg)
11
- @value = Value.(arg)
12
- end
13
-
14
- def value_or(method_name = UNDEFINED, &block)
15
- raise NotImplementedError
16
- end
17
-
18
- def none?
19
- raise NotImplementedError
20
- end
21
-
22
- def some?; !none?; end
23
-
24
- def map(&fn)
25
- raise NotImplementedError
26
- end
27
-
28
- alias_method :map!, :map
29
- alias_method :then, :map
30
- alias_method :then!, :map
31
-
32
- def check(&fn)
33
- raise NotImplementedError
34
- end
35
-
36
- def try(method_name = UNDEFINED, &block)
37
- raise NotImplementedError
38
- end
39
-
40
- alias_method :try!, :try
41
-
42
- def dig(*keys)
43
- raise NotImplementedError
44
- end
45
-
46
- def presence
47
- raise NotImplementedError
48
- end
49
- end
50
- end
51
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Maybe
5
- class Some < Result
6
- def value_or(default = UNDEFINED, &block)
7
- @value
8
- end
9
-
10
- def none?; false; end
11
-
12
- def map(&fn)
13
- map!(&fn)
14
- rescue StandardError => exception
15
- None.new(exception)
16
- end
17
-
18
- alias_method :then, :map
19
-
20
- def check(&fn)
21
- result = fn.call(@value)
22
-
23
- !result || KIND.null?(result) ? NONE_WITH_NIL_VALUE : self
24
- end
25
-
26
- def map!(&fn)
27
- result = fn.call(@value)
28
-
29
- resolve(result)
30
- end
31
-
32
- alias_method :then!, :map!
33
-
34
- def try!(method_name = UNDEFINED, *args, &block)
35
- return __try_block__(block, args) if block
36
-
37
- return __try_method__(method_name, args) if UNDEFINED != method_name
38
-
39
- raise ArgumentError, 'method name or a block must be present'
40
- end
41
-
42
- def try(method_name = UNDEFINED, *args, &block)
43
- return __try_block__(block, args) if block
44
-
45
- return __try_method__(method_name, args) if value.respond_to?(method_name)
46
-
47
- NONE_WITH_NIL_VALUE
48
- end
49
-
50
- def dig(*keys)
51
- resolve(Dig.(value, keys))
52
- end
53
-
54
- def presence
55
- resolve(Presence.(value))
56
- end
57
-
58
- private
59
-
60
- def __try_method__(method_name, args)
61
- __try_block__(KIND.of!(::Symbol, method_name).to_proc, args)
62
- end
63
-
64
- def __try_block__(block, args)
65
- result = args.empty? ? block.call(value) : block.call(*args.unshift(value))
66
-
67
- resolve(result)
68
- end
69
-
70
- def resolve(result)
71
- return result if Maybe::None === result
72
- return None.new(result) if Exception === result
73
- return NONE_WITH_NIL_VALUE if result.nil?
74
- return NONE_WITH_UNDEFINED_VALUE if Undefined == result
75
-
76
- Some.new(result)
77
- end
78
- end
79
-
80
- VALUE_CANT_BE_NONE = "value can't be nil or Kind::Undefined".freeze
81
-
82
- def self.some(value)
83
- return Maybe::Some.new(value) if !KIND.null?(value)
84
-
85
- raise ArgumentError, VALUE_CANT_BE_NONE
86
- end
87
-
88
- private_constant :VALUE_CANT_BE_NONE
89
- end
90
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Maybe
5
- class Typed
6
- include Wrappable
7
-
8
- def initialize(kind)
9
- @kind = kind
10
- end
11
-
12
- def new(arg)
13
- value = Result::Value.(arg)
14
-
15
- @kind === value ? Maybe.some(value) : Maybe.none
16
- end
17
-
18
- alias_method :[], :new
19
-
20
- private
21
-
22
- def __call_before_expose_the_arg_in_a_block(arg)
23
- value = Result::Value.(arg)
24
-
25
- @kind === value ? value : Maybe.none
26
- end
27
- end
28
- end
29
- end
data/lib/kind/core/try.rb DELETED
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module Try
5
- extend self
6
-
7
- def call(*args)
8
- object = args.shift
9
-
10
- call!(object, args.shift, args)
11
- end
12
-
13
- def self.[](*args)
14
- method_name = args.shift
15
-
16
- ->(object) { call!(object, method_name, args) }
17
- end
18
-
19
- def call!(object, method_name, args) # :nodoc
20
- return if KIND.null?(object)
21
-
22
- resolve(object, method_name, args)
23
- end
24
-
25
- private
26
-
27
- def resolve(object, method_name, args = Empty::ARRAY)
28
- return unless object.respond_to?(method_name)
29
- return object.public_send(method_name) if args.empty?
30
-
31
- object.public_send(method_name, *args)
32
- end
33
- end
34
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module TypeChecker
5
- def name
6
- kind.name
7
- end
8
-
9
- def ===(value)
10
- kind === value
11
- end
12
-
13
- def [](value)
14
- return value if self === value
15
-
16
- KIND.error!(name, value)
17
- end
18
-
19
- def or_nil(value)
20
- return value if self === value
21
- end
22
-
23
- def or_undefined(value)
24
- or_nil(value) || Undefined
25
- end
26
-
27
- def or(fallback, value = UNDEFINED)
28
- return __or_func.(fallback) if UNDEFINED === value
29
-
30
- self === value ? value : fallback
31
- end
32
-
33
- def value?(value = UNDEFINED)
34
- return self === value if UNDEFINED != value
35
-
36
- @__is_value ||= ->(tc) { ->(arg) { tc === arg } }.(self)
37
- end
38
-
39
- def value(arg, default:)
40
- KIND.value(self, arg, self[default])
41
- end
42
-
43
- def or_null(value) # :nodoc:
44
- KIND.null?(value) ? value : self[value]
45
- end
46
-
47
- def maybe(value = UNDEFINED, &block)
48
- return __maybe[value] if UNDEFINED != value && !block
49
- return __maybe.wrap(&block) if UNDEFINED == value && block
50
- return __maybe.wrap(value, &block) if UNDEFINED != value && block
51
-
52
- __maybe
53
- end
54
-
55
- alias optional maybe
56
-
57
- private
58
-
59
- def __maybe
60
- @__maybe ||= Maybe::Typed.new(self)
61
- end
62
-
63
- def __or_func
64
- @__or_func ||= ->(tc, fb, value) { tc === value ? value : tc.or_null(fb) }.curry[self]
65
- end
66
- end
67
-
68
- class TypeChecker::Object # :nodoc: all
69
- include TypeChecker
70
-
71
- ResolveKindName = ->(kind, opt) do
72
- name = Try.(opt, :[], :name)
73
- name || Try.(kind, :name)
74
- end
75
-
76
- attr_reader :kind, :name
77
-
78
- def initialize(kind, opt)
79
- name = ResolveKindName.(kind, opt)
80
-
81
- @name = KIND.of!(::String, name)
82
- @kind = KIND.respond_to!(:===, kind)
83
- end
84
-
85
- private_constant :ResolveKindName
86
- end
87
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'kind/core/type_checkers/ruby/array'
4
- require 'kind/core/type_checkers/ruby/class'
5
- require 'kind/core/type_checkers/ruby/comparable'
6
- require 'kind/core/type_checkers/ruby/enumerable'
7
- require 'kind/core/type_checkers/ruby/enumerator'
8
- require 'kind/core/type_checkers/ruby/file'
9
- require 'kind/core/type_checkers/ruby/float'
10
- require 'kind/core/type_checkers/ruby/hash'
11
- require 'kind/core/type_checkers/ruby/integer'
12
- require 'kind/core/type_checkers/ruby/io'
13
- require 'kind/core/type_checkers/ruby/module'
14
- require 'kind/core/type_checkers/ruby/method'
15
- require 'kind/core/type_checkers/ruby/numeric'
16
- require 'kind/core/type_checkers/ruby/proc'
17
- require 'kind/core/type_checkers/ruby/queue'
18
- require 'kind/core/type_checkers/ruby/range'
19
- require 'kind/core/type_checkers/ruby/regexp'
20
- require 'kind/core/type_checkers/ruby/string'
21
- require 'kind/core/type_checkers/ruby/struct'
22
- require 'kind/core/type_checkers/ruby/symbol'
23
- require 'kind/core/type_checkers/ruby/time'
24
-
25
- require 'kind/core/type_checkers/stdlib/open_struct'
26
- require 'kind/core/type_checkers/stdlib/set'
27
-
28
- require 'kind/core/type_checkers/custom/boolean'
29
- require 'kind/core/type_checkers/custom/callable'
30
- require 'kind/core/type_checkers/custom/lambda'