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
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Maybe
5
- module Wrappable
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.__none__(exception)
15
+ Maybe::None.new(exception)
16
16
  end
17
17
  else
18
18
  return new(arg) if UNDEFINED != arg
19
19
 
20
- raise ArgumentError, 'wrong number of arguments (given 0, expected 1)'
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 :Wrappable
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kind/maybe'
4
+ require 'kind/either'
5
+ require 'kind/result'
@@ -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, TypeChecker
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,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Class
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Class; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Comparable
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Comparable; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Enumerable
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Enumerable; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Enumerator
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Enumerator; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module File
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::File; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Float
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Float; end
8
8
  end
@@ -2,13 +2,15 @@
2
2
 
3
3
  module Kind
4
4
  module Hash
5
- extend self, TypeChecker
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,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Integer
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Integer; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module IO
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::IO; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Method
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Method; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Module
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Module; end
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Numeric
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Numeric; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Proc
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Proc; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Queue
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Queue; end
8
8
  def name; 'Queue'; end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Range
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Range; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Regexp
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Regexp; end
8
8
  end
@@ -2,13 +2,15 @@
2
2
 
3
3
  module Kind
4
4
  module String
5
- extend self, TypeChecker
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)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Struct
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Struct; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Symbol
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Symbol; end
8
8
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Time
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Time; end
8
8
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Kind
4
4
  module Boolean
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
- def kind; [TrueClass, FalseClass].freeze; end
7
+ def kind; [TrueClass, FalseClass]; end
8
8
 
9
9
  def name; 'Boolean'; end
10
10
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Callable
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; raise NotImplementedError; end
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kind
4
4
  module Lambda
5
- extend self, TypeChecker
5
+ extend self, ::Kind::Object
6
6
 
7
7
  def kind; ::Proc; end
8
8
 
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ostruct'
4
+
3
5
  module Kind
4
6
  module OpenStruct
5
- extend self, TypeChecker
7
+ extend self, ::Kind::Object
6
8
 
7
9
  def kind; ::OpenStruct; end
8
10
  end