kind 4.1.0 → 5.4.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 (113) hide show
  1. checksums.yaml +4 -4
  2. data/.tool-versions +1 -1
  3. data/.travis.sh +39 -7
  4. data/.travis.yml +1 -2
  5. data/CHANGELOG.md +524 -29
  6. data/Gemfile +13 -6
  7. data/README.md +57 -43
  8. data/kind.gemspec +3 -3
  9. data/lib/kind.rb +2 -84
  10. data/lib/kind/__lib__/action_steps.rb +57 -0
  11. data/lib/kind/__lib__/attributes.rb +66 -0
  12. data/lib/kind/__lib__/kind.rb +51 -0
  13. data/lib/kind/__lib__/of.rb +17 -0
  14. data/lib/kind/__lib__/strict.rb +49 -0
  15. data/lib/kind/__lib__/undefined.rb +14 -0
  16. data/lib/kind/action.rb +127 -0
  17. data/lib/kind/active_model/validation.rb +3 -4
  18. data/lib/kind/basic.rb +79 -0
  19. data/lib/kind/basic/error.rb +29 -0
  20. data/lib/kind/{undefined.rb → basic/undefined.rb} +6 -1
  21. data/lib/kind/dig.rb +21 -5
  22. data/lib/kind/either.rb +30 -0
  23. data/lib/kind/either/left.rb +29 -0
  24. data/lib/kind/either/methods.rb +17 -0
  25. data/lib/kind/either/monad.rb +65 -0
  26. data/lib/kind/either/monad/wrapper.rb +19 -0
  27. data/lib/kind/either/right.rb +38 -0
  28. data/lib/kind/empty.rb +2 -2
  29. data/lib/kind/empty/constant.rb +7 -0
  30. data/lib/kind/enum.rb +63 -0
  31. data/lib/kind/enum/item.rb +40 -0
  32. data/lib/kind/enum/methods.rb +72 -0
  33. data/lib/kind/function.rb +45 -0
  34. data/lib/kind/functional.rb +89 -0
  35. data/lib/kind/functional/action.rb +87 -0
  36. data/lib/kind/functional/steps.rb +22 -0
  37. data/lib/kind/immutable_attributes.rb +34 -0
  38. data/lib/kind/immutable_attributes/initializer.rb +70 -0
  39. data/lib/kind/immutable_attributes/reader.rb +38 -0
  40. data/lib/kind/maybe.rb +37 -12
  41. data/lib/kind/maybe/methods.rb +21 -0
  42. data/lib/kind/maybe/monad.rb +82 -0
  43. data/lib/kind/maybe/monad/wrapper.rb +19 -0
  44. data/lib/kind/maybe/none.rb +12 -19
  45. data/lib/kind/maybe/some.rb +68 -26
  46. data/lib/kind/maybe/typed.rb +11 -5
  47. data/lib/kind/maybe/{wrappable.rb → wrapper.rb} +8 -4
  48. data/lib/kind/monad.rb +22 -0
  49. data/lib/kind/monads.rb +5 -0
  50. data/lib/kind/objects.rb +17 -0
  51. data/lib/kind/objects/basic_object.rb +43 -0
  52. data/lib/kind/objects/modules.rb +32 -0
  53. data/lib/kind/{type_checkers → objects/modules}/core/array.rb +3 -1
  54. data/lib/kind/{type_checkers → objects/modules}/core/class.rb +1 -1
  55. data/lib/kind/{type_checkers → objects/modules}/core/comparable.rb +1 -1
  56. data/lib/kind/{type_checkers → objects/modules}/core/enumerable.rb +1 -1
  57. data/lib/kind/{type_checkers → objects/modules}/core/enumerator.rb +1 -1
  58. data/lib/kind/{type_checkers → objects/modules}/core/file.rb +1 -1
  59. data/lib/kind/{type_checkers → objects/modules}/core/float.rb +1 -1
  60. data/lib/kind/{type_checkers → objects/modules}/core/hash.rb +3 -1
  61. data/lib/kind/{type_checkers → objects/modules}/core/integer.rb +1 -1
  62. data/lib/kind/{type_checkers → objects/modules}/core/io.rb +1 -1
  63. data/lib/kind/{type_checkers → objects/modules}/core/method.rb +1 -1
  64. data/lib/kind/{type_checkers → objects/modules}/core/module.rb +1 -1
  65. data/lib/kind/{type_checkers → objects/modules}/core/numeric.rb +1 -1
  66. data/lib/kind/{type_checkers → objects/modules}/core/proc.rb +1 -1
  67. data/lib/kind/{type_checkers → objects/modules}/core/queue.rb +1 -1
  68. data/lib/kind/{type_checkers → objects/modules}/core/range.rb +1 -1
  69. data/lib/kind/{type_checkers → objects/modules}/core/regexp.rb +1 -1
  70. data/lib/kind/{type_checkers → objects/modules}/core/string.rb +3 -1
  71. data/lib/kind/{type_checkers → objects/modules}/core/struct.rb +1 -1
  72. data/lib/kind/{type_checkers → objects/modules}/core/symbol.rb +1 -1
  73. data/lib/kind/{type_checkers → objects/modules}/core/time.rb +1 -1
  74. data/lib/kind/{type_checkers → objects/modules}/custom/boolean.rb +2 -2
  75. data/lib/kind/{type_checkers → objects/modules}/custom/callable.rb +1 -1
  76. data/lib/kind/{type_checkers → objects/modules}/custom/lambda.rb +1 -1
  77. data/lib/kind/{type_checkers → objects/modules}/stdlib/open_struct.rb +3 -1
  78. data/lib/kind/{type_checkers → objects/modules}/stdlib/set.rb +3 -1
  79. data/lib/kind/objects/nil.rb +17 -0
  80. data/lib/kind/objects/not_nil.rb +9 -0
  81. data/lib/kind/objects/object.rb +56 -0
  82. data/lib/kind/objects/respond_to.rb +30 -0
  83. data/lib/kind/objects/union_type.rb +44 -0
  84. data/lib/kind/presence.rb +4 -2
  85. data/lib/kind/result.rb +31 -0
  86. data/lib/kind/result/abstract.rb +53 -0
  87. data/lib/kind/result/failure.rb +33 -0
  88. data/lib/kind/result/methods.rb +17 -0
  89. data/lib/kind/result/monad.rb +74 -0
  90. data/lib/kind/result/monad/wrapper.rb +19 -0
  91. data/lib/kind/result/success.rb +53 -0
  92. data/lib/kind/strict/disabled.rb +34 -0
  93. data/lib/kind/try.rb +22 -10
  94. data/lib/kind/validator.rb +111 -0
  95. data/lib/kind/version.rb +1 -1
  96. metadata +84 -51
  97. data/lib/kind/active_model/kind_validator.rb +0 -103
  98. data/lib/kind/core.rb +0 -10
  99. data/lib/kind/core/deprecation.rb +0 -29
  100. data/lib/kind/core/kind.rb +0 -61
  101. data/lib/kind/core/undefined.rb +0 -7
  102. data/lib/kind/core/wrong_number_of_args.rb +0 -9
  103. data/lib/kind/deprecations/built_in_type_checkers.rb +0 -23
  104. data/lib/kind/deprecations/checker.rb +0 -16
  105. data/lib/kind/deprecations/checker/factory.rb +0 -31
  106. data/lib/kind/deprecations/checker/protocol.rb +0 -73
  107. data/lib/kind/deprecations/is.rb +0 -35
  108. data/lib/kind/deprecations/of.rb +0 -258
  109. data/lib/kind/deprecations/types.rb +0 -121
  110. data/lib/kind/error.rb +0 -15
  111. data/lib/kind/maybe/result.rb +0 -51
  112. data/lib/kind/type_checker.rb +0 -87
  113. data/lib/kind/type_checkers.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffb9807682a295f94cb8e4076d32fc3217da97268e943e7d6b028b6de95ed765
4
- data.tar.gz: 7b61164996c0d96853ad5a0cde9269ecd18bb489b483b46956a7fa2e9b9f2cd6
3
+ metadata.gz: e9a47a1176ad4b10eaad08e63af26b3067fd6d058c16e592b34f01c0f540bcbb
4
+ data.tar.gz: ef1859a67157e0e515213cf2e544d959df292eba3bb7371595de4df76d035379
5
5
  SHA512:
6
- metadata.gz: 0f23b38b9f63d03638ded172c037b6ba9e048c397e9a31907850beca5151822c4983129e5726b777ccbdb30db18c8bcdfb9392c813c3e05546dfd33f97db9660
7
- data.tar.gz: 3d7e663d65a591bff7bfeb152dd4e605948df7e95de15bb3632be0f52c747fd522041fc1be7678c8385cd9c0606a688abd203348e376ce264f3088d62ecb5f88
6
+ metadata.gz: 3004e273ebd8aaa9807cbfe87e728d13f5a01792ab1206480618ff49bf1405938b8e62d5da10a3dbbcd81b49382eb0b684f55dd3e00f27ee7de97200c9496984
7
+ data.tar.gz: b42bc24083b5edb01409449f0222a525ba0106fca3afed8f43b2a21c10f597b9be6d8fcf8e4e0e4f5a73b706e8439affae3c14ef330258e4ef5adb630c1a9863
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 2.7.2
1
+ ruby 3.0.0
data/.travis.sh CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  RUBY_V=$(ruby -v)
4
4
 
5
+ function run_basic_tests {
6
+ if [ ! -z "$1" ]; then
7
+ bundle_cmd="bundle _$1_"
8
+ else
9
+ bundle_cmd="bundle"
10
+ fi
11
+
12
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/{basic/*_test,basic_test}.rb'"
13
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/enum_test.rb'"
14
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/presence_test.rb'"
15
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/dig_test.rb'"
16
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/try_test.rb'"
17
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/maybe_test.rb'"
18
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/immutable_attributes_test.rb'"
19
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/function_test.rb'"
20
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/action_test.rb'"
21
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/{functional/*_test,functional_test}.rb'"
22
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/either/*_test.rb'"
23
+ eval "KIND_BASIC=t $bundle_cmd exec rake test TEST='test/kind/result/*_test.rb'"
24
+
25
+ eval "KIND_STRICT=t $bundle_cmd exec rake test TEST='test/kind/strict_disabled_test.rb'"
26
+ }
27
+
5
28
  function run_with_bundler {
6
29
  rm Gemfile.lock
7
30
 
@@ -19,12 +42,17 @@ function run_with_am_version_and_bundler {
19
42
  run_with_bundler "$2" "ACTIVEMODEL_VERSION=$1"
20
43
  }
21
44
 
45
+ RUBY_2_12="ruby 2.[12]."
22
46
  RUBY_2_2345="ruby 2.[2345]."
47
+ RUBY_2_12345="ruby 2.[12345]."
48
+ RUBY_2_567="ruby 2.[567]."
49
+ RUBY_3_0="ruby 3.0."
23
50
 
24
- if [[ $RUBY_V =~ $RUBY_2_2345 ]]; then
25
- run_with_bundler "$BUNDLER_V1"
26
-
51
+ if [[ $RUBY_V =~ $RUBY_2_12 ]]; then
27
52
  run_with_am_version_and_bundler "3.2" "$BUNDLER_V1"
53
+ fi
54
+
55
+ if [[ $RUBY_V =~ $RUBY_2_2345 ]]; then
28
56
  run_with_am_version_and_bundler "4.0" "$BUNDLER_V1"
29
57
  run_with_am_version_and_bundler "4.1" "$BUNDLER_V1"
30
58
  run_with_am_version_and_bundler "4.2" "$BUNDLER_V1"
@@ -33,13 +61,17 @@ if [[ $RUBY_V =~ $RUBY_2_2345 ]]; then
33
61
  run_with_am_version_and_bundler "5.2" "$BUNDLER_V1"
34
62
  fi
35
63
 
36
- RUBY_2_567="ruby 2.[567]."
37
- RUBY_3_x_x="ruby 3.0."
64
+ if [[ $RUBY_V =~ $RUBY_2_12345 ]]; then
65
+ run_basic_tests "$BUNDLER_V1"
66
+ run_with_bundler "$BUNDLER_V1"
67
+ fi
38
68
 
39
- if [[ $RUBY_V =~ $RUBY_2_567 ]] || [[ $RUBY_V =~ $RUBY_3_x_x ]]; then
69
+ if [[ $RUBY_V =~ $RUBY_2_567 ]] || [[ $RUBY_V =~ $RUBY_3_0 ]]; then
40
70
  gem install bundler -v ">= 2" --no-doc
41
71
 
42
- run_with_bundler
43
72
  run_with_am_version_and_bundler "6.0"
44
73
  run_with_am_version_and_bundler "6.1"
74
+
75
+ run_basic_tests
76
+ run_with_bundler
45
77
  fi
data/.travis.yml CHANGED
@@ -1,14 +1,13 @@
1
1
  ---
2
2
  language: ruby
3
3
 
4
- sudo: false
5
-
6
4
  cache:
7
5
  bundler: true
8
6
  directories:
9
7
  - /home/travis/.rvm/
10
8
 
11
9
  rvm:
10
+ - 2.1.10
12
11
  - 2.2.2
13
12
  - 2.3.0
14
13
  - 2.4.0
data/CHANGELOG.md CHANGED
@@ -3,63 +3,77 @@
3
3
  This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the recommendations of [keepachangelog.com](http://keepachangelog.com/).
4
4
 
5
5
  - [Unreleased](#unreleased)
6
- - [4.1.0 (2021-02-22)](#410-2021-02-22)
6
+ - [5.4.0 (2021-03-25)](#540-2021-03-25)
7
7
  - [Added](#added)
8
- - [4.0.0 (2021-02-22)](#400-2021-02-22)
8
+ - [5.3.0 (2021-03-23)](#530-2021-03-23)
9
9
  - [Added](#added-1)
10
- - [Deprecated](#deprecated)
11
- - [Fixed](#fixed)
12
- - [3.1.0 (2020-07-08)](#310-2020-07-08)
10
+ - [5.2.0 (2021-03-17)](#520-2021-03-17)
13
11
  - [Added](#added-2)
14
- - [3.0.0 (2020-06-25)](#300-2020-06-25)
15
- - [Breaking Changes](#breaking-changes)
12
+ - [Deprecated](#deprecated)
13
+ - [Changes](#changes)
14
+ - [5.1.0 (2021-02-23)](#510-2021-02-23)
16
15
  - [Added](#added-3)
17
- - [2.3.0 (2020-06-24)](#230-2020-06-24)
16
+ - [Deprecated](#deprecated-1)
17
+ - [5.0.0 (2021-02-22)](#500-2021-02-22)
18
+ - [Breaking Changes](#breaking-changes)
19
+ - [Removed](#removed)
20
+ - [4.1.0 (2021-02-22)](#410-2021-02-22)
18
21
  - [Added](#added-4)
19
- - [2.2.0 (2020-06-23)](#220-2020-06-23)
22
+ - [4.0.0 (2021-02-22)](#400-2021-02-22)
20
23
  - [Added](#added-5)
21
- - [2.1.0 (2020-05-12)](#210-2020-05-12)
24
+ - [Deprecated](#deprecated-2)
25
+ - [Fixed](#fixed)
26
+ - [3.1.0 (2020-07-08)](#310-2020-07-08)
22
27
  - [Added](#added-6)
28
+ - [3.0.0 (2020-06-25)](#300-2020-06-25)
23
29
  - [Breaking Changes](#breaking-changes-1)
24
- - [2.0.0 (2020-05-07)](#200-2020-05-07)
25
30
  - [Added](#added-7)
31
+ - [2.3.0 (2020-06-24)](#230-2020-06-24)
32
+ - [Added](#added-8)
33
+ - [2.2.0 (2020-06-23)](#220-2020-06-23)
34
+ - [Added](#added-9)
35
+ - [2.1.0 (2020-05-12)](#210-2020-05-12)
36
+ - [Added](#added-10)
26
37
  - [Breaking Changes](#breaking-changes-2)
27
- - [Removed](#removed)
38
+ - [2.0.0 (2020-05-07)](#200-2020-05-07)
39
+ - [Added](#added-11)
40
+ - [Breaking Changes](#breaking-changes-3)
41
+ - [Removed](#removed-1)
28
42
  - [1.9.0 (2020-05-06)](#190-2020-05-06)
29
- - [Added](#added-8)
43
+ - [Added](#added-12)
30
44
  - [1.8.0 (2020-05-03)](#180-2020-05-03)
31
- - [Added](#added-9)
45
+ - [Added](#added-13)
32
46
  - [1.7.0 (2020-05-03)](#170-2020-05-03)
33
47
  - [Fixed](#fixed-1)
34
48
  - [1.6.0 (2020-04-17)](#160-2020-04-17)
35
- - [Added](#added-10)
36
- - [Changes](#changes)
49
+ - [Added](#added-14)
50
+ - [Changes](#changes-1)
37
51
  - [1.5.0 (2020-04-12)](#150-2020-04-12)
38
- - [Added](#added-11)
52
+ - [Added](#added-15)
39
53
  - [1.4.0 (2020-04-12)](#140-2020-04-12)
40
- - [Added](#added-12)
54
+ - [Added](#added-16)
41
55
  - [1.3.0 (2020-04-12)](#130-2020-04-12)
42
- - [Added](#added-13)
56
+ - [Added](#added-17)
43
57
  - [1.2.0 (2020-04-12)](#120-2020-04-12)
44
- - [Added](#added-14)
58
+ - [Added](#added-18)
45
59
  - [1.1.0 (2020-04-09)](#110-2020-04-09)
46
- - [Added](#added-15)
60
+ - [Added](#added-19)
47
61
  - [Fixed](#fixed-2)
48
62
  - [1.0.0 (2020-03-16)](#100-2020-03-16)
49
- - [Added](#added-16)
63
+ - [Added](#added-20)
50
64
  - [0.6.0 (2020-01-06)](#060-2020-01-06)
51
- - [Added](#added-17)
65
+ - [Added](#added-21)
52
66
  - [0.5.0 (2020-01-04)](#050-2020-01-04)
53
- - [Added](#added-18)
67
+ - [Added](#added-22)
54
68
  - [0.4.0 (2020-01-03)](#040-2020-01-03)
55
- - [Added](#added-19)
69
+ - [Added](#added-23)
56
70
  - [0.3.0 (2020-01-03)](#030-2020-01-03)
57
- - [Added](#added-20)
58
- - [Breaking Changes](#breaking-changes-3)
71
+ - [Added](#added-24)
72
+ - [Breaking Changes](#breaking-changes-4)
59
73
  - [0.2.0 (2020-01-02)](#020-2020-01-02)
60
- - [Added](#added-21)
74
+ - [Added](#added-25)
61
75
  - [0.1.0 (2019-12-26)](#010-2019-12-26)
62
- - [Added](#added-22)
76
+ - [Added](#added-26)
63
77
 
64
78
  ## Unreleased
65
79
 
@@ -71,6 +85,487 @@ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
71
85
  ### Fixed
72
86
  -->
73
87
 
88
+ 5.4.0 (2021-03-25)
89
+ ------------------
90
+
91
+ * [#54](https://github.com/serradura/kind/pull/54) - Add `Kind::Functional::Steps` to allow the usage of `Step`, `Map`, `Try`, `Tee`, `Check`, `Success` and `Failure` in any kind of object.
92
+ ```ruby
93
+ # Usage in classes' instances
94
+
95
+ class BaseJob
96
+ def self.perform_now(input); new.perform(input); end
97
+
98
+ def perform(input); raise NotImplementedError; end
99
+ end
100
+
101
+ class CreateUserJob < BaseJob
102
+ include Kind::Functional::Steps
103
+
104
+ def perform(input)
105
+ validate(input) \
106
+ >> Step(:create) \
107
+ >> Step(:welcome_email)
108
+ end
109
+
110
+ private
111
+
112
+ def validate(input)
113
+ # Success() or Failure()
114
+ end
115
+
116
+ def create(input)
117
+ # Success() or Failure()
118
+ end
119
+
120
+ def welcome_email(email)
121
+ # Success() or Failure()
122
+ end
123
+ end
124
+
125
+ # Usage in modules (singleton methods)
126
+
127
+ module CreateUser
128
+ extend self, Kind::Functional::Steps
129
+
130
+ def perform(input)
131
+ Step!(:validate, input) \
132
+ >> Step(:create) \
133
+ >> Step(:welcome_email)
134
+ end
135
+
136
+ private
137
+
138
+ def validate(input)
139
+ # Success() or Failure()
140
+ end
141
+
142
+ def create(input)
143
+ # Success() or Failure()
144
+ end
145
+
146
+ def welcome_email(email)
147
+ # Success() or Failure()
148
+ end
149
+ end
150
+ ```
151
+
152
+ [⬆️ &nbsp;Back to Top](#changelog-)
153
+
154
+ ### Added
155
+
156
+ 5.3.0 (2021-03-23)
157
+ ------------------
158
+
159
+ ### Added
160
+
161
+ * [#53](https://github.com/serradura/kind/pull/53) - Allow `Kind::Result#map` and `Kind::Result#map!` receive a callable as an argument.
162
+
163
+ * [#53](https://github.com/serradura/kind/pull/53) - Add `|` and `>>` as an alias of `Kind::Result#map!`.
164
+
165
+ * [#53](https://github.com/serradura/kind/pull/53) - Add step adapters for `Kind::Action` and `Kind::Functional::Action`. This is the list of methods: `Step`, `Map`, `Try`, `Tee`, `Check`.
166
+ ```ruby
167
+ module CreateUser
168
+ extend Kind::Functional::Action
169
+
170
+ def call!(input)
171
+ Step!(:validate, input) \
172
+ | Step(:create)
173
+ end
174
+
175
+ private
176
+
177
+ def validate(input)
178
+ # returns Success(valid_data) or Failure(validation)
179
+ end
180
+
181
+ def create(input)
182
+ # returns Success(user)
183
+ end
184
+ end
185
+ ```
186
+
187
+ * [#53](https://github.com/serradura/kind/pull/53) - Add `kind/strict/disabled` to turn off all of the strict validations and optimize the runtime. Use case: As strict validation is useful in development, this mechanism could be used to optimize the runtime in production. List of methods that will be disabled:
188
+ * `Kind.of()`
189
+ * `Kind.of_class()`
190
+ * `Kind.of_module()`
191
+ * `Kind.of_module_or_class()`
192
+ * `Kind::<Type>[1]`
193
+ * `Kind::NotNil[1]`
194
+
195
+ [⬆️ &nbsp;Back to Top](#changelog-)
196
+
197
+ 5.2.0 (2021-03-17)
198
+ ------------------
199
+
200
+ ### Added
201
+
202
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind.respond_to?`. This method has two different behaviors, when it receives just one argument it will behave like the native Ruby's
203
+ `respond_to?` method, that is, it will check if the `Kind` method implements some method. But if it receives two arguments, the first one will be the object, and the others (one or more) will be a list of method names that will be used to check if the given object implements them. e.g.
204
+ ```ruby
205
+ Kind.respond_to?(:is?) # true
206
+
207
+ Kind.respond_to?(:foo?) # true
208
+
209
+ # --
210
+
211
+ Kind.respond_to?({}, :fetch, :merge) # true
212
+
213
+ Kind.respond_to?([], :fetch, :merge) # false
214
+ ```
215
+
216
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::UnionType`. This class allows the creation of an object that knows how to compare a value through different kinds of types. e.g.
217
+ ```ruby
218
+ # The method [] can build a union type object that will have the method #|
219
+ # which allows the composition with other object kinds.
220
+ array_or_hash = Kind::UnionType[Array] | Hash
221
+
222
+ # The method === can verify if a given value is one of the kinds that compounds the union type.
223
+ array_or_hash === {} # true
224
+ array_or_hash === [] # true
225
+ array_or_hash === 1 # false
226
+ array_or_hash === nil # false
227
+
228
+ # The method #[] will return the given value if it has one of the expected kinds,
229
+ # but if not, it will raise a Kind::Error.
230
+ array_or_hash[{}] # {}
231
+
232
+ array_or_hash[1] # Kind::Error (1 expected to be a kind of (Array | Hash))
233
+
234
+ # At last, the method #name is an alias to the method #inspect.
235
+ array_or_hash.name # "(Array | Hash)"
236
+ array_or_hash.inspect # "(Array | Hash)"
237
+ ```
238
+
239
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Nil`. This module was added to be used to create union types. e.g.
240
+ ```ruby
241
+ hash_or_nil = Kind::UnionType[Hash] | Kind::Nil
242
+
243
+ hash_or_nil === {} # true
244
+ hash_or_nil === [] # false
245
+ hash_or_nil === 1 # false
246
+ hash_or_nil === nil # true
247
+ ```
248
+
249
+ * [#46](https://github.com/serradura/kind/pull/46), [#47](https://github.com/serradura/kind/pull/47) - Add `Kind::NotNil`. This module was added to perform a strict verification where the given value will be returned if it is not nil, and if not, a `Kind::Error` will be raised. e.g.
250
+ ```ruby
251
+ Kind::NotNil[1] # 1
252
+
253
+ Kind::NotNil[nil] # Kind::Error (expected to not be nil)
254
+
255
+ Kind::NotNil[nil, label: 'Foo#bar'] # Kind::Error (Foo#bar: expected to not be nil)
256
+ ```
257
+
258
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::RespondTo` to create objects that know how to verify if a given object implements one or more expected methods.
259
+ ```ruby
260
+ HashLike = Kind::RespondTo[:fetch, :merge!]
261
+ Fetchable = Kind::RespondTo[:fetch]
262
+
263
+ # Verifying if an object implements the expected interface.
264
+ HashLike === ENV # true
265
+ HashLike === {} # true
266
+ HashLike === [] # false
267
+
268
+ Fetchable === ENV # true
269
+ Fetchable === [] # true
270
+ Fetchable === {} # true
271
+
272
+ # Performing an strict verification
273
+ HashLike[ENV] # true
274
+ HashLike[{}] # true
275
+ HashLike[Array.new] # false Kind::Error ([] expected to be a kind of Kind::RespondTo[:fetch, :merge!])
276
+
277
+ # Creating union types using interfaces
278
+ HashLikeOrArray = HashLike | Array # Kind::RespondTo[:fetch, :merge!] | Array
279
+
280
+ HashLikeOrArray === {} # true
281
+ HashLikeOrArray === [] # true
282
+ HashLikeOrArray === ENV # true
283
+ ```
284
+
285
+ * [#46](https://github.com/serradura/kind/pull/46) - Unfreeze the output of `Kind::Boolean.kind`.
286
+
287
+ * [#46](https://github.com/serradura/kind/pull/46) - Freeze `Kind::UNDEFINED` and define its inspect method.
288
+
289
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::TypeChecker#|` to allow the creation of union types.
290
+ ```ruby
291
+ StatusLabel = Kind::String | Kind::Symbol
292
+
293
+ StatusLabel === :ok # true
294
+ StatusLabel === 'ok' # true
295
+ StatusLabel === true # false
296
+ ```
297
+
298
+ * [#46](https://github.com/serradura/kind/pull/46) - Allow `Kind.of()` and `Kind::TypeChecker#[]` receive a label.
299
+ ```ruby
300
+ # Kind.of(<Type>, value, label:)
301
+ class Person
302
+ attr_reader :name
303
+
304
+ def initialize(name:)
305
+ @name = Kind.of(String, name, label: 'Person#name')
306
+ end
307
+ end
308
+
309
+ Person.new(name: 'Rodrigo') # #<Person:0x0000... @name="Rodrigo">
310
+ Person.new(name: :ok) # Kind::Error (Person#name: :ok expected to be a kind of String)
311
+
312
+ # Kind<Type>[value, label:]
313
+ class User
314
+ attr_reader :name
315
+
316
+ def initialize(name:)
317
+ @name = Kind::String[name, label: 'User#name']
318
+ end
319
+ end
320
+
321
+ User.new(name: 'Rodrigo') # #<User:0x0000... @name="Rodrigo">
322
+ User.new(name: :ok) # Kind::Error (User#name: :ok expected to be a kind of String)
323
+ ```
324
+
325
+ * [#46](https://github.com/serradura/kind/pull/46) - Create `kind/basic` to expose a small set of features to do type handling/verification. This is util if you want to make use only of these features instead of all of the others that are loaded via `require "kind"`. So, use `require "kind/basic"` if you only want these features:
326
+ * `Kind.is?`
327
+ * `Kind.of`
328
+ * `Kind.of?`
329
+ * `Kind.of_class?`
330
+ * `Kind.of_module?`
331
+ * `Kind.of_module_or_class`
332
+ * `Kind.respond_to`
333
+ * `Kind.respond_to?`
334
+ * `Kind.value`
335
+ * `Kind::Error`
336
+ * `Kind::Undefined`
337
+
338
+ * [#46](https://github.com/serradura/kind/pull/46) - Improve `Kind::Maybe`.
339
+ * Improve the `#inspect` output.
340
+ * Make `Kind::Maybe.{new,[],wrap}` return `None` if they receive an exception instance.
341
+ * Add `#accept` as an alias of `#check` method.
342
+ * Add `#reject` as the reverse of the methods `#check` and `#accept`.
343
+ * Allow the methods `#map`, `#map!`, `#then`, `#then!`, `#check`, `#accept`, `#reject` to receive one symbol as an argument, it will be used to perform the correspondent method in the `Maybe` value, so if the object responds to the expected method a `Some` will be returned.
344
+ * Add `Kind::Maybe#on`, this method allows you to use a block where will be possible to define a `Some` or `None` handling. The output of the matched result (some or none) will be the block's output.
345
+ ```ruby
346
+ # Kind::Maybe#accept (an alias of Kind::Maybe#check)
347
+ Kind::Maybe[1].accept(&:odd?) # #<Kind::Some value=1>
348
+ Kind::Maybe[1].accept(&:even?) # #<Kind::None value=nil>
349
+
350
+ # Kind::Maybe#reject (the reverse of Kind::Maybe#check)
351
+ Kind::Maybe[1].reject(&:odd?) # #<Kind::None value=nil>
352
+ Kind::Maybe[1].reject(&:even?) # #<Kind::Some value=1>
353
+
354
+ # Passing one symbol as an argument of the methods: `#map`, `#then`, `#check`, `#accept`, `#reject`
355
+ Kind::Maybe['1'].map(:to_i) # #<Kind::Some value=1>
356
+ Kind::Maybe[' '].then(:strip) # #<Kind::Some value="">
357
+
358
+ Kind::Maybe['1'].map!(:to_i).accept(:odd?) # #<Kind::Some value=1>
359
+ Kind::Maybe[' '].then!(:strip).reject(:empty?) # #<Kind::None value=nil>
360
+
361
+ # `Kind::Maybe#on` making use of a block to handle Some or None results.
362
+ number = Kind::Maybe['2'].then(:to_i).reject(:even?).on do |result|
363
+ result.none { 0 }
364
+ result.some { 1 }
365
+ end
366
+
367
+ p number # 0
368
+ ```
369
+
370
+ [⬆️ &nbsp;Back to Top](#changelog-)
371
+
372
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Either` (either monad). This is basically the same as the `Maybe` monad, but with `Some` called `Right` and `None` called `Left`. But this time, `Left` is also allowed to have an underlying value. This module isn't loaded by default, so you will need to require it.
373
+ ```ruby
374
+ require 'kind/either'
375
+
376
+ # Use the methods Kind::Left() or Kind::Right() to create either monads
377
+ Kind::Left(0) # #<Kind::Left value=0>
378
+ Kind::Right(1) # #<Kind::Right value=1>
379
+
380
+ # The Kind::Either.new() or Kind::Either[] also creates a Kind::Right monad
381
+ Kind::Either.new(2) # #<Kind::Right value=2>
382
+ Kind::Either[3] # #<Kind::Right value=3>
383
+
384
+ # An Either has methods that allow you to know what kind it is.
385
+ monad = Kind::Right(1)
386
+ monad.right? # true
387
+ monad.left? # false
388
+
389
+ # This monad allows you to chain a sequence of operations that will continue while the output
390
+ # of each step is a Right monad. So, if some step return a Left, all of the next steps will be avoided.
391
+ # Let's see an example of how you can use the method #map to define a sequence of operations.
392
+
393
+ def do_some_calculation(arg)
394
+ Kind::Right(arg)
395
+ .map { |value| Kind::Numeric?(value) ? Kind::Right(value + 2) : Kind::Left('value must be numeric') }
396
+ .map { |value| value.odd? ? Kind::Right(value) : Kind::Left('value can\'t be even') }
397
+ .map { |value| Kind::Right(value * 3) }
398
+ end
399
+
400
+ do_some_calculation('1') # #<Kind::Left value="value must be numeric">
401
+ do_some_calculation(2) # #<Kind::Left value="value can't be even">
402
+ do_some_calculation(1) # #<Kind::Right value=9>
403
+
404
+ # You can use procs/lambdas as an alternative of blocks
405
+ Add = ->(a, b) do
406
+ return Kind::Right(a + b) if Kind::Numeric?(a, b)
407
+
408
+ Kind::Left('the arguments must be numerics')
409
+ end
410
+
411
+ Double = ->(number) do
412
+ return Kind::Right(number * 2) if Kind::Numeric?(number)
413
+
414
+ Kind::Left('the argument must be numeric')
415
+ end
416
+
417
+ AsString = ->(value) { Kind::Right(value.to_s) }
418
+
419
+ Add.(1, 2).map(&Double).map(&Double) # #<Kind::Right value=12>
420
+ Add.(1, 2).map(&AsString).map(&Double) # #<Kind::Left value="the argument must be numeric">
421
+
422
+ # The method #then is an alias for #map
423
+ Add.(2, 2).then(&Double).then(&AsString) # #<Kind::Right value="8">
424
+
425
+ # An exception will occur when your block or lambda doesn't return a Kind::Either
426
+ Add.(2, 2).map { |number| number * 2 } # Kind::Monad::Error (8 expected to be a kind of Kind::Right | Kind::Left)
427
+
428
+ # The methods #map, #then auto handle StandardError exceptions,
429
+ # so Left will be returned when an exception occur.
430
+ Add.(0, 0).map { |number| Kind::Right(10 / number) } # #<Kind::Left value=#<ZeroDivisionError: divided by 0>>
431
+
432
+ # You can use #map! or #then! if you don't want this auto exception handling.
433
+ Add.(0, 0).map! { |number| Kind::Right(10 / number) } # ZeroDivisionError (divided by 0)
434
+
435
+ Add.(0, 0).then! { |number| Kind::Right(10 / number) } # ZeroDivisionError (divided by 0)
436
+ ```
437
+
438
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Result` (an Either monad variation)
439
+
440
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Function`.
441
+
442
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Functional`.
443
+
444
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Try.presence` and improve the input/output handling of `Kind::Try.call`.
445
+
446
+ * [#46](https://github.com/serradura/kind/pull/46) - Add `Kind::Dig.presence` and improve the input/output handling of `Kind::Dig.call`.
447
+
448
+ * [#47](https://github.com/serradura/kind/pull/47) - Add `Kind.is!`
449
+
450
+ * [#47](https://github.com/serradura/kind/pull/47) - Create aliases to the methods `Kind.of` (`Kind.of!`) and `Kind.respond_to` (`Kind.respond_to!`)
451
+
452
+ * [#47](https://github.com/serradura/kind/pull/47) - Add `Kind[]` as the `Kind::Of()` substitute.
453
+
454
+ * [#49](https://github.com/serradura/kind/pull/49) - Add `Kind::Either::Methods` and `Kind::Result::Methods`
455
+
456
+ * [#49](https://github.com/serradura/kind/pull/49) - Add `Kind::Undefined.empty?`
457
+
458
+ * [#50](https://github.com/serradura/kind/pull/50) - Add `Kind::<Type>.empty_or` as an alias of `Kind::<Type>.value_or_empty`
459
+
460
+ * [#46](https://github.com/serradura/kind/pull/46), [#51](https://github.com/serradura/kind/pull/51) - Add `Kind::Functional::Action`.
461
+
462
+ * [#51](https://github.com/serradura/kind/pull/51) - Add `Kind::Action`.
463
+
464
+ * [#51](https://github.com/serradura/kind/pull/51) - Add `Kind::Maybe::ImmutableAttributes`.
465
+
466
+ * [#51](https://github.com/serradura/kind/pull/51) - Add `Kind::Maybe::Methods`.
467
+
468
+ * [#51](https://github.com/serradura/kind/pull/51) - Add `Kind::Enum`.
469
+
470
+ * [#51](https://github.com/serradura/kind/pull/51) - Modularize all the kind components. So now you can require only the modules/features that do you want, `kind/action` is the minimal requirement for all of them.
471
+ * `kind/action`
472
+ * `kind/dig`
473
+ * `kind/either`
474
+ * `kind/empty`
475
+ * `kind/enum`
476
+ * `kind/function`
477
+ * `kind/functional`
478
+ * `kind/functional/action`
479
+ * `kind/immutable_attributes`
480
+ * `kind/maybe`
481
+ * `kind/objects`
482
+ * `kind/presence`
483
+ * `kind/result`
484
+ * `kind/try`
485
+ * `kind/validator`
486
+
487
+ * [#52](https://github.com/serradura/kind/pull/52) - Improve `Kind::Validator`, now will be possible to make use of `lambdas` or objects that responds to `.===` and `.name` to perform a kind validation. e.g.
488
+ ```ruby
489
+ class User
490
+ include ActiveModel::Validations
491
+
492
+ attr_reader :name, :bool
493
+
494
+ FilledString = ->(value) { value.kind_of?(String) && !value.empty? }
495
+
496
+ Bool = Object.new
497
+ def Bool.===(value)
498
+ value == true || value == false
499
+ end
500
+ def Bool.name; 'Bool'; end
501
+
502
+ validates :name, kind: FilledString
503
+ validates :bool, kind: Bool
504
+
505
+ def initialize(name:, bool:)
506
+ @name, @bool = name, bool
507
+ end
508
+ end
509
+
510
+ user = User.new(name: '', bool: 1)
511
+
512
+ user.valid? # true
513
+ user.errors[:name] # ['invalid kind']
514
+ user.errors[:bool] # ['must be a kind of Bool']
515
+
516
+ User.new(name: 'Serradura', bool: true).valid? # true
517
+ ```
518
+
519
+ * [#52](https://github.com/serradura/kind/pull/52) - Add `Kind::Enum.===`.
520
+
521
+ ### Deprecated
522
+
523
+ * [#47](https://github.com/serradura/kind/pull/47) - Deprecate `Kind.is` and `Kind::Of()`.
524
+
525
+ * [#48](https://github.com/serradura/kind/pull/48) - Deprecate `Kind::Maybe()` and `Kind::Optional()`
526
+
527
+ ### Changes
528
+
529
+ * [#48](https://github.com/serradura/kind/pull/48) - Rename `Kind::TypeChecker` to `Kind::Object` and `Kind::TypeChecker::Object` to `Kind::Object::Instance`.
530
+
531
+ [⬆️ &nbsp;Back to Top](#changelog-)
532
+
533
+ 5.1.0 (2021-02-23)
534
+ ------------------
535
+
536
+ ### Added
537
+
538
+ * [#45](https://github.com/serradura/kind/pull/45) - Add support to Ruby `>= 2.1.0`.
539
+
540
+ ### Deprecated
541
+
542
+ * [#45](https://github.com/serradura/kind/pull/45) - `kind/active_model/validation` is deprecated; use `kind/validator` instead.
543
+
544
+ 5.0.0 (2021-02-22)
545
+ ------------------
546
+
547
+ ### Breaking Changes
548
+
549
+ * [#44](https://github.com/serradura/kind/pull/44) - Now, do you need to require `"kind/empty/constant"` to define the constant `Empty` as a `Kind::Empty` alias.
550
+
551
+ ### Removed
552
+
553
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Is.call`
554
+
555
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Of.call`
556
+
557
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Types.add`.
558
+
559
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Of::<Type>` and `Kind::Is::<Type>` modules.
560
+
561
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Checker`, `Kind::Checker::Protocol`, `Kind::Checker::Factory`.
562
+
563
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.is` without arguments.
564
+
565
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.of` without arguments.
566
+
567
+ * [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.of` with a single argument (the kind).
568
+
74
569
  4.1.0 (2021-02-22)
75
570
  ------------------
76
571