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