kind 5.1.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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'
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- module KIND
5
- def self.null?(value) # :nodoc:
6
- value.nil? || Undefined == value
7
- end
8
-
9
- def self.of?(kind, values) # :nodoc:
10
- of_kind = -> value { kind === value }
11
-
12
- values.empty? ? of_kind : values.all?(&of_kind)
13
- end
14
-
15
- def self.of!(kind, value, kind_name = nil) # :nodoc:
16
- return value if kind === value
17
-
18
- error!(kind_name || kind.name, value)
19
- end
20
-
21
- def self.error!(kind_name, value) # :nodoc:
22
- raise Error.new(kind_name, value)
23
- end
24
-
25
- def self.of_class?(value) # :nodoc:
26
- value.kind_of?(::Class)
27
- end
28
-
29
- def self.of_module?(value) # :nodoc:
30
- ::Module == value || (value.kind_of?(::Module) && !of_class?(value))
31
- end
32
-
33
- def self.of_module_or_class!(value) # :nodoc:
34
- of!(::Module, value, 'Module/Class')
35
- end
36
-
37
- def self.respond_to!(method_name, value) # :nodoc:
38
- return value if value.respond_to?(method_name)
39
-
40
- raise Error.new("expected #{value} to respond to :#{method_name}")
41
- end
42
-
43
- def self.is?(expected, value) # :nodoc:
44
- is!(of_module_or_class!(expected), value)
45
- end
46
-
47
- def self.is!(expected_kind, value) # :nodoc:
48
- kind = of_module_or_class!(value)
49
-
50
- if of_class?(kind)
51
- kind <= expected_kind || expected_kind == ::Class
52
- else
53
- kind == expected_kind || kind.kind_of?(expected_kind)
54
- end
55
- end
56
-
57
- def self.value(kind, arg, default) # :nodoc:
58
- kind === arg ? arg : default
59
- end
60
- end
61
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kind
4
- UNDEFINED = Object.new.freeze
5
-
6
- private_constant :UNDEFINED
7
- end