kind 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -23
- data/lib/kind/is.rb +5 -1
- data/lib/kind/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 273fcdcef40d8b61e552bb440f93a7b83adfd29621e1809201a66117babf9974
|
4
|
+
data.tar.gz: 84dac931b67ccc4ee895a8c2a4c3326ccfe0faf0540df09ba9fbcbfbe5034edc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3d702df70066307c7dac3058c41dab688de46c606d57455194d77570823d051dab5fd43b5c87c870dee58b9cef41a9e93a7d9d4639fe39489cc4b0e2d93d4bf
|
7
|
+
data.tar.gz: 4c02cd4e43a0407167f4284c6972feed925380a0d71a8c5dedcb474a24f458954175a929dac9d5b4509520cfea663e45692dba67269712bc6e8d83e53b80a8aa
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Basic type system for Ruby.
|
|
12
12
|
|
13
13
|
As a creator of Ruby gems, I have a common need that I have to handle in many of my projects: type checking of method arguments.
|
14
14
|
|
15
|
-
One of the goals of this project is to do simple type checking like `"some string".is_a?(String)`, but using a bunch of basic abstractions. So, after reading this README and realizing that you need something more robust, I recommend
|
15
|
+
One of the goals of this project is to do simple type checking like `"some string".is_a?(String)`, but using a bunch of basic abstractions. So, after reading this README and realizing that you need something more robust, I recommend you check out the [dry-types gem](https://dry-rb.org/gems/dry-types).
|
16
16
|
|
17
17
|
## Table of Contents <!-- omit in toc -->
|
18
18
|
- [Required Ruby version](#required-ruby-version)
|
@@ -25,18 +25,17 @@ One of the goals of this project is to do simple type checking like `"some strin
|
|
25
25
|
- [Registering new (custom) type checkers](#registering-new-custom-type-checkers)
|
26
26
|
- [What happens if a custom type checker has a namespace?](#what-happens-if-a-custom-type-checker-has-a-namespace)
|
27
27
|
- [Type checkers](#type-checkers)
|
28
|
-
- [Classes' type
|
29
|
-
- [
|
30
|
-
- [
|
31
|
-
- [Kind.of](#kindof)
|
28
|
+
- [Classes' type checkers](#classes-type-checkers)
|
29
|
+
- [Modules' type checkers](#modules-type-checkers)
|
30
|
+
- [Specials' type checkers](#specials-type-checkers)
|
32
31
|
- [Kind::Undefined](#kindundefined)
|
33
|
-
- [Kind.of
|
32
|
+
- [Kind.of.\<Type\>.or_undefined()](#kindoftypeor_undefined)
|
34
33
|
- [Kind::Maybe](#kindmaybe)
|
35
34
|
- [Kind::Maybe[] and Kind::Maybe#then](#kindmaybe-and-kindmaybethen)
|
36
35
|
- [Kind::Maybe#try](#kindmaybetry)
|
37
36
|
- [Kind.of.Maybe()](#kindofmaybe)
|
38
37
|
- [Kind::Optional](#kindoptional)
|
39
|
-
|
38
|
+
- [Kind::Empty](#kindempty)
|
40
39
|
- [Development](#development)
|
41
40
|
- [Contributing](#contributing)
|
42
41
|
- [License](#license)
|
@@ -82,7 +81,7 @@ sum('1', 1) # Kind::Error ("\"1\" expected to be a kind of Numeric")
|
|
82
81
|
By default, basic verifications are strict. So, when you perform `Kind.of.Hash(value)`, if the given value was a Hash, the value itself will be returned, but if it isn't the right type, an error will be raised.
|
83
82
|
|
84
83
|
```ruby
|
85
|
-
Kind.of.Hash(nil) # raise Kind::Error, "nil expected to be a kind of Hash"
|
84
|
+
Kind.of.Hash(nil) # **raise Kind::Error, "nil expected to be a kind of Hash"**
|
86
85
|
Kind.of.Hash('') # raise Kind::Error, "'' expected to be a kind of Hash"
|
87
86
|
Kind.of.Hash({a: 1}) # {a: 1}
|
88
87
|
|
@@ -105,9 +104,9 @@ Kind.of.Hash(value, or: {}) # {}
|
|
105
104
|
Kind.of.Boolean(nil, or: true) # true
|
106
105
|
```
|
107
106
|
|
108
|
-
As an alternative syntax, you can use the `Kind::Of` instead of the method. e.g: `Kind::Of::Hash('')`
|
107
|
+
As an alternative syntax, you can use the `Kind::Of` instead of the `Kind.of` method. e.g: `Kind::Of::Hash('')`
|
109
108
|
|
110
|
-
But if you don't need a strict type verification, use the `.or_nil`method
|
109
|
+
But if you don't need a strict type verification, use the `.or_nil` method.
|
111
110
|
|
112
111
|
```ruby
|
113
112
|
Kind.of.Hash.or_nil('') # nil
|
@@ -206,8 +205,8 @@ kind_of_user.instance?(User) # true
|
|
206
205
|
kind_of_user.class?(Hash) # false
|
207
206
|
kind_of_user.class?(User) # true
|
208
207
|
|
209
|
-
#
|
210
|
-
# because
|
208
|
+
# Creating type checkers dynamically is cheap
|
209
|
+
# because a singleton object is created to be available for use.
|
211
210
|
|
212
211
|
kind_of_user.object_id == Kind.of(User).object_id # true
|
213
212
|
|
@@ -215,10 +214,10 @@ kind_of_user.object_id == Kind.of(User).object_id # true
|
|
215
214
|
# Kind.is() can be used to check a class/module #
|
216
215
|
# --------------------------------------------- #
|
217
216
|
|
218
|
-
class
|
217
|
+
class AdminUser < User
|
219
218
|
end
|
220
219
|
|
221
|
-
Kind.is(
|
220
|
+
Kind.is(User, AdminUser) # true
|
222
221
|
```
|
223
222
|
|
224
223
|
#### Registering new (custom) type checkers
|
@@ -229,7 +228,7 @@ Use `Kind::Types.add()`. e.g:
|
|
229
228
|
class User
|
230
229
|
end
|
231
230
|
|
232
|
-
# You can define it at the end of the file
|
231
|
+
# You can define it at the end of the file class/module where class/module was declared.
|
233
232
|
|
234
233
|
Kind::Types.add(User)
|
235
234
|
|
@@ -310,7 +309,7 @@ Kind.of.Account::User::Membership.class?(Account::User::Membership) # true
|
|
310
309
|
|
311
310
|
The list of types (classes and modules) available to use with `Kind.of.*` or `Kind.is.*` are:
|
312
311
|
|
313
|
-
|
312
|
+
### Classes' type checkers
|
314
313
|
|
315
314
|
- `Kind.of.String`
|
316
315
|
- `Kind.of.Symbol`
|
@@ -330,14 +329,12 @@ The list of types (classes and modules) available to use with `Kind.of.*` or `Ki
|
|
330
329
|
- `Kind.of.IO`
|
331
330
|
- `Kind.of.File`
|
332
331
|
|
333
|
-
###
|
332
|
+
### Modules' type checkers
|
334
333
|
|
335
334
|
- `Kind.of.Enumerable`
|
336
335
|
- `Kind.of.Comparable`
|
337
336
|
|
338
|
-
###
|
339
|
-
|
340
|
-
#### Kind.of
|
337
|
+
### Specials' type checkers
|
341
338
|
|
342
339
|
- `Kind.of.Class()`
|
343
340
|
- `Kind.of.Module()`
|
@@ -356,7 +353,7 @@ The [`Kind::Undefined`](https://github.com/serradura/kind/blob/834f6b8ebdc737de8
|
|
356
353
|
|
357
354
|
If you are interested, check out [the tests](https://github.com/serradura/kind/blob/834f6b8ebdc737de8e5628986585f30c1a5aa41b/test/kind/undefined_test.rb) to understand its methods.
|
358
355
|
|
359
|
-
### Kind.of
|
356
|
+
### Kind.of.\<Type\>.or_undefined()
|
360
357
|
|
361
358
|
If you interested in use `Kind::Undefined` you can use the method `.or_undefined` with any of the [available type checkers](#type-checkers). e.g:
|
362
359
|
|
@@ -521,9 +518,9 @@ PS: The `Kind.of.Optional` is available to check if some value is a `Kind::Optio
|
|
521
518
|
|
522
519
|
[⬆️ Back to Top](#table-of-contents-)
|
523
520
|
|
524
|
-
|
521
|
+
## Kind::Empty
|
525
522
|
|
526
|
-
|
523
|
+
When you define a method that has default arguments, for certain data types, you will always create a new object in memory. e.g:
|
527
524
|
|
528
525
|
```ruby
|
529
526
|
def something(params = {})
|
data/lib/kind/is.rb
CHANGED
@@ -9,7 +9,11 @@ module Kind
|
|
9
9
|
def self.__call__(expected_kind, object)
|
10
10
|
kind = Kind::Of.Module(object)
|
11
11
|
|
12
|
-
kind
|
12
|
+
if kind.is_a?(Class)
|
13
|
+
kind <= expected_kind || false
|
14
|
+
else
|
15
|
+
kind == expected_kind || kind.is_a?(expected_kind)
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
data/lib/kind/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kind
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Basic type system for Ruby (free of dependencies).
|
14
14
|
email:
|