kind 5.1.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.tool-versions +1 -1
- data/.travis.sh +35 -10
- data/CHANGELOG.md +364 -26
- data/README.md +37 -36
- data/lib/kind.rb +2 -49
- data/lib/kind/__lib__/attributes.rb +66 -0
- data/lib/kind/__lib__/kind.rb +71 -0
- data/lib/kind/__lib__/undefined.rb +14 -0
- data/lib/kind/action.rb +92 -0
- data/lib/kind/active_model/validation.rb +1 -1
- data/lib/kind/basic.rb +73 -0
- data/lib/kind/basic/error.rb +29 -0
- data/lib/kind/{core → basic}/undefined.rb +6 -1
- data/lib/kind/{core/dig.rb → dig.rb} +20 -4
- data/lib/kind/either.rb +30 -0
- data/lib/kind/either/left.rb +29 -0
- data/lib/kind/either/methods.rb +17 -0
- data/lib/kind/either/monad.rb +65 -0
- data/lib/kind/either/monad/wrapper.rb +19 -0
- data/lib/kind/either/right.rb +38 -0
- data/lib/kind/{core/empty.rb → empty.rb} +2 -0
- data/lib/kind/{core/empty → empty}/constant.rb +0 -0
- data/lib/kind/enum.rb +63 -0
- data/lib/kind/enum/item.rb +40 -0
- data/lib/kind/enum/methods.rb +72 -0
- data/lib/kind/function.rb +47 -0
- data/lib/kind/functional.rb +89 -0
- data/lib/kind/functional/action.rb +89 -0
- data/lib/kind/immutable_attributes.rb +34 -0
- data/lib/kind/immutable_attributes/initializer.rb +70 -0
- data/lib/kind/immutable_attributes/reader.rb +38 -0
- data/lib/kind/maybe.rb +69 -0
- data/lib/kind/maybe/methods.rb +21 -0
- data/lib/kind/maybe/monad.rb +82 -0
- data/lib/kind/maybe/monad/wrapper.rb +19 -0
- data/lib/kind/{core/maybe → maybe}/none.rb +11 -18
- data/lib/kind/maybe/some.rb +132 -0
- data/lib/kind/maybe/typed.rb +35 -0
- data/lib/kind/{core/maybe/wrappable.rb → maybe/wrapper.rb} +8 -4
- data/lib/kind/monad.rb +22 -0
- data/lib/kind/monads.rb +5 -0
- data/lib/kind/objects.rb +17 -0
- data/lib/kind/objects/basic_object.rb +45 -0
- data/lib/kind/objects/modules.rb +32 -0
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/array.rb +3 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/class.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/comparable.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerable.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/enumerator.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/file.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/float.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/hash.rb +3 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/integer.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/io.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/method.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/module.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/numeric.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/proc.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/queue.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/range.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/regexp.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/string.rb +3 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/struct.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/symbol.rb +1 -1
- data/lib/kind/{core/type_checkers/ruby → objects/modules/core}/time.rb +1 -1
- data/lib/kind/{core/type_checkers → objects/modules}/custom/boolean.rb +2 -2
- data/lib/kind/{core/type_checkers → objects/modules}/custom/callable.rb +1 -1
- data/lib/kind/{core/type_checkers → objects/modules}/custom/lambda.rb +1 -1
- data/lib/kind/{core/type_checkers → objects/modules}/stdlib/open_struct.rb +3 -1
- data/lib/kind/{core/type_checkers → objects/modules}/stdlib/set.rb +3 -1
- data/lib/kind/objects/nil.rb +17 -0
- data/lib/kind/objects/not_nil.rb +13 -0
- data/lib/kind/objects/object.rb +56 -0
- data/lib/kind/objects/respond_to.rb +30 -0
- data/lib/kind/objects/union_type.rb +44 -0
- data/lib/kind/{core/presence.rb → presence.rb} +4 -2
- data/lib/kind/result.rb +31 -0
- data/lib/kind/result/abstract.rb +53 -0
- data/lib/kind/result/failure.rb +31 -0
- data/lib/kind/result/methods.rb +17 -0
- data/lib/kind/result/monad.rb +69 -0
- data/lib/kind/result/monad/wrapper.rb +19 -0
- data/lib/kind/result/success.rb +40 -0
- data/lib/kind/try.rb +46 -0
- data/lib/kind/validator.rb +14 -10
- data/lib/kind/version.rb +1 -1
- metadata +81 -47
- data/lib/kind/core.rb +0 -15
- data/lib/kind/core/error.rb +0 -15
- data/lib/kind/core/maybe.rb +0 -42
- data/lib/kind/core/maybe/result.rb +0 -51
- data/lib/kind/core/maybe/some.rb +0 -90
- data/lib/kind/core/maybe/typed.rb +0 -29
- data/lib/kind/core/try.rb +0 -34
- data/lib/kind/core/type_checker.rb +0 -87
- data/lib/kind/core/type_checkers.rb +0 -30
- data/lib/kind/core/utils/kind.rb +0 -61
- data/lib/kind/core/utils/undefined.rb +0 -7
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Kind
|
4
4
|
module Maybe
|
5
|
-
module
|
5
|
+
module Wrapper
|
6
6
|
def wrap(arg = UNDEFINED)
|
7
7
|
if block_given?
|
8
8
|
begin
|
@@ -12,15 +12,19 @@ module Kind
|
|
12
12
|
|
13
13
|
input.kind_of?(Maybe::None) ? input : new(yield(input))
|
14
14
|
rescue StandardError => exception
|
15
|
-
Maybe.
|
15
|
+
Maybe::None.new(exception)
|
16
16
|
end
|
17
17
|
else
|
18
18
|
return new(arg) if UNDEFINED != arg
|
19
19
|
|
20
|
-
|
20
|
+
Error.wrong_number_of_args!(given: 0, expected: 1)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
def from(&block)
|
25
|
+
wrap(&block)
|
26
|
+
end
|
27
|
+
|
24
28
|
private
|
25
29
|
|
26
30
|
def __call_before_expose_the_arg_in_a_block(input)
|
@@ -28,6 +32,6 @@ module Kind
|
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
|
-
private_constant :
|
35
|
+
private_constant :Wrapper
|
32
36
|
end
|
33
37
|
end
|
data/lib/kind/monad.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kind
|
4
|
+
module Monad
|
5
|
+
Error = ::Class.new(Kind::Error)
|
6
|
+
|
7
|
+
class Wrapper
|
8
|
+
def initialize(monad)
|
9
|
+
@monad = monad
|
10
|
+
@output = UNDEFINED
|
11
|
+
end
|
12
|
+
|
13
|
+
def output?
|
14
|
+
UNDEFINED != @output
|
15
|
+
end
|
16
|
+
|
17
|
+
def output
|
18
|
+
@output if output?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/kind/monads.rb
ADDED
data/lib/kind/objects.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'kind/try'
|
4
|
+
require 'kind/maybe'
|
5
|
+
|
6
|
+
require 'kind/objects/basic_object'
|
7
|
+
require 'kind/objects/respond_to'
|
8
|
+
require 'kind/objects/union_type'
|
9
|
+
require 'kind/objects/nil'
|
10
|
+
require 'kind/objects/not_nil'
|
11
|
+
require 'kind/objects/object'
|
12
|
+
require 'kind/objects/modules'
|
13
|
+
|
14
|
+
module Kind
|
15
|
+
UnionType.send(:include, Maybe::Buildable)
|
16
|
+
RespondTo.send(:include, Maybe::Buildable)
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kind
|
4
|
+
module BasicObject
|
5
|
+
def [](value, label: nil)
|
6
|
+
return value if self === value
|
7
|
+
|
8
|
+
KIND.error!(name, value, label)
|
9
|
+
end
|
10
|
+
|
11
|
+
def or_nil(value)
|
12
|
+
return value if self === value
|
13
|
+
end
|
14
|
+
|
15
|
+
def or_undefined(value)
|
16
|
+
or_nil(value) || Undefined
|
17
|
+
end
|
18
|
+
|
19
|
+
def or(fallback, value = UNDEFINED)
|
20
|
+
return __or_func.(fallback) if UNDEFINED === value
|
21
|
+
|
22
|
+
self === value ? value : fallback
|
23
|
+
end
|
24
|
+
|
25
|
+
def value?(value = UNDEFINED)
|
26
|
+
return self === value if UNDEFINED != value
|
27
|
+
|
28
|
+
@__is_value ||= ->(tc) { ->(arg) { tc === arg } }.(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def value(arg, default:)
|
32
|
+
KIND.value(self, arg, self[default])
|
33
|
+
end
|
34
|
+
|
35
|
+
def or_null(value) # :nodoc:
|
36
|
+
KIND.null?(value) ? value : self[value]
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def __or_func
|
42
|
+
@__or_func ||= ->(tc, fb, value) { tc === value ? value : tc.or_null(fb) }.curry[self]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'kind/empty'
|
4
|
+
|
5
|
+
require 'kind/objects/modules/core/array'
|
6
|
+
require 'kind/objects/modules/core/class'
|
7
|
+
require 'kind/objects/modules/core/comparable'
|
8
|
+
require 'kind/objects/modules/core/enumerable'
|
9
|
+
require 'kind/objects/modules/core/enumerator'
|
10
|
+
require 'kind/objects/modules/core/file'
|
11
|
+
require 'kind/objects/modules/core/float'
|
12
|
+
require 'kind/objects/modules/core/hash'
|
13
|
+
require 'kind/objects/modules/core/integer'
|
14
|
+
require 'kind/objects/modules/core/io'
|
15
|
+
require 'kind/objects/modules/core/module'
|
16
|
+
require 'kind/objects/modules/core/method'
|
17
|
+
require 'kind/objects/modules/core/numeric'
|
18
|
+
require 'kind/objects/modules/core/proc'
|
19
|
+
require 'kind/objects/modules/core/queue'
|
20
|
+
require 'kind/objects/modules/core/range'
|
21
|
+
require 'kind/objects/modules/core/regexp'
|
22
|
+
require 'kind/objects/modules/core/string'
|
23
|
+
require 'kind/objects/modules/core/struct'
|
24
|
+
require 'kind/objects/modules/core/symbol'
|
25
|
+
require 'kind/objects/modules/core/time'
|
26
|
+
|
27
|
+
require 'kind/objects/modules/stdlib/open_struct'
|
28
|
+
require 'kind/objects/modules/stdlib/set'
|
29
|
+
|
30
|
+
require 'kind/objects/modules/custom/boolean'
|
31
|
+
require 'kind/objects/modules/custom/callable'
|
32
|
+
require 'kind/objects/modules/custom/lambda'
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
module Kind
|
4
4
|
module Array
|
5
|
-
extend self,
|
5
|
+
extend self, ::Kind::Object
|
6
6
|
|
7
7
|
def kind; ::Array; end
|
8
8
|
|
9
9
|
def value_or_empty(arg)
|
10
10
|
KIND.value(self, arg, Empty::ARRAY)
|
11
11
|
end
|
12
|
+
|
13
|
+
alias empty_or value_or_empty
|
12
14
|
end
|
13
15
|
|
14
16
|
def self.Array?(*values)
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
module Kind
|
4
4
|
module Hash
|
5
|
-
extend self,
|
5
|
+
extend self, ::Kind::Object
|
6
6
|
|
7
7
|
def kind; ::Hash; end
|
8
8
|
|
9
9
|
def value_or_empty(arg)
|
10
10
|
KIND.value(self, arg, Empty::HASH)
|
11
11
|
end
|
12
|
+
|
13
|
+
alias empty_or value_or_empty
|
12
14
|
end
|
13
15
|
|
14
16
|
def self.Hash?(*values)
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
module Kind
|
4
4
|
module String
|
5
|
-
extend self,
|
5
|
+
extend self, ::Kind::Object
|
6
6
|
|
7
7
|
def kind; ::String; end
|
8
8
|
|
9
9
|
def value_or_empty(arg)
|
10
10
|
KIND.value(self, arg, Empty::STRING)
|
11
11
|
end
|
12
|
+
|
13
|
+
alias empty_or value_or_empty
|
12
14
|
end
|
13
15
|
|
14
16
|
def self.String?(*values)
|