kind 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/README.md +52 -46
- data/lib/kind.rb +14 -12
- 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: 8d3ff3a2a9d5b61087a96cf0255208086b5338f2a5f5ccb4ca609a9c8cff01e5
|
4
|
+
data.tar.gz: 10d77db1c01fb6990056fbb1b3a2dde463b8627af1f5aa415b7b11a89b76c367
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef9bd5cfb51763cb15690e232e60b474c6f958f5d2de7faab9d5688ce1f9e9b74c529b2bc2fbae39082e7bc0ea13b5d95038d6fd488f0a8d96d576e4843eb5c4
|
7
|
+
data.tar.gz: 515db015436abfe14cc392d46cdb41e8590af11c1d5e8e12466a4d85bfcd8681759e40f20ffbd9bac44a859a9b658e37c9218f2eec9bcc9118f68d3f755b8c56
|
data/.travis.yml
CHANGED
@@ -17,4 +17,12 @@ before_install:
|
|
17
17
|
|
18
18
|
install: bundle install --jobs=3 --retry=3
|
19
19
|
|
20
|
+
before_script:
|
21
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
22
|
+
- chmod +x ./cc-test-reporter
|
23
|
+
- "./cc-test-reporter before-build"
|
24
|
+
|
20
25
|
script: bundle exec rake test
|
26
|
+
|
27
|
+
after_success:
|
28
|
+
- "./cc-test-reporter after-build -t simplecov"
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
![Ruby](https://img.shields.io/badge/ruby-2.2+-ruby.svg?colorA=99004d&colorB=cc0066)
|
2
2
|
[![Gem](https://img.shields.io/gem/v/kind.svg?style=flat-square)](https://rubygems.org/gems/kind)
|
3
3
|
[![Build Status](https://travis-ci.com/serradura/kind.svg?branch=master)](https://travis-ci.com/serradura/kind)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/711329decb2806ccac95/maintainability)](https://codeclimate.com/github/serradura/kind/maintainability)
|
5
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/711329decb2806ccac95/test_coverage)](https://codeclimate.com/github/serradura/kind/test_coverage)
|
4
6
|
|
5
7
|
# Kind <!-- omit in toc -->
|
6
8
|
|
@@ -18,12 +20,12 @@ One of the goals of this project is to do simple type checking like `"some strin
|
|
18
20
|
- [Usage](#usage)
|
19
21
|
- [Verifying the kind of some object](#verifying-the-kind-of-some-object)
|
20
22
|
- [Verifying the kind of some class/module](#verifying-the-kind-of-some-classmodule)
|
23
|
+
- [How to create a new type checker?](#how-to-create-a-new-type-checker)
|
24
|
+
- [What happens if a custom type checker has a namespace?](#what-happens-if-a-custom-type-checker-has-a-namespace)
|
21
25
|
- [Built-in type checkers](#built-in-type-checkers)
|
22
26
|
- [Special type checkers](#special-type-checkers)
|
23
27
|
- [Kind.of](#kindof)
|
24
28
|
- [Kind.is](#kindis)
|
25
|
-
- [How to create a new type checker?](#how-to-create-a-new-type-checker)
|
26
|
-
- [What happens if a custom type checker has a namespace?](#what-happens-if-a-custom-type-checker-has-a-namespace)
|
27
29
|
- [Development](#development)
|
28
30
|
- [Contributing](#contributing)
|
29
31
|
- [License](#license)
|
@@ -59,9 +61,9 @@ def sum(a, b)
|
|
59
61
|
Kind.of.Numeric(a) + Kind.of.Numeric(b)
|
60
62
|
end
|
61
63
|
|
62
|
-
sum(1, 1)
|
64
|
+
sum(1, 1) # 2
|
63
65
|
|
64
|
-
sum('1', 1 # Kind::Error ("\"1\" expected to be a kind of Numeric")
|
66
|
+
sum('1', 1) # Kind::Error ("\"1\" expected to be a kind of Numeric")
|
65
67
|
```
|
66
68
|
|
67
69
|
### Verifying the kind of some object
|
@@ -69,6 +71,8 @@ sum('1', 1 # Kind::Error ("\"1\" expected to be a kind of Numeric")
|
|
69
71
|
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.
|
70
72
|
|
71
73
|
```ruby
|
74
|
+
Kind.of.Hash(nil) # raise Kind::Error, "nil expected to be a kind of Hash"
|
75
|
+
|
72
76
|
Kind.of.Hash('') # raise Kind::Error, "'' expected to be a kind of Hash"
|
73
77
|
|
74
78
|
Kind.of.Hash({a: 1}) # {a: 1}
|
@@ -145,47 +149,7 @@ Kind.of.Hash.class?(ActiveSupport::HashWithIndifferentAccess) # true
|
|
145
149
|
|
146
150
|
[⬆️ Back to Top](#table-of-contents-)
|
147
151
|
|
148
|
-
|
149
|
-
|
150
|
-
The list of types (classes and modules) available to use with `Kind.of.*` or `Kind.is.*` are:
|
151
|
-
|
152
|
-
| Classes | Modules |
|
153
|
-
| ---------- | ---------- |
|
154
|
-
| String | Enumerable |
|
155
|
-
| Symbol | Comparable |
|
156
|
-
| Numeric | |
|
157
|
-
| Integer | |
|
158
|
-
| Float | |
|
159
|
-
| Regexp | |
|
160
|
-
| Time | |
|
161
|
-
| Array | |
|
162
|
-
| Range | |
|
163
|
-
| Hash | |
|
164
|
-
| Struct | |
|
165
|
-
| Enumerator | |
|
166
|
-
| Method | |
|
167
|
-
| Proc | |
|
168
|
-
| IO | |
|
169
|
-
| File | |
|
170
|
-
|
171
|
-
### Special type checkers
|
172
|
-
|
173
|
-
#### Kind.of
|
174
|
-
|
175
|
-
- `Kind.of.Class()`
|
176
|
-
- `Kind.of.Module()`
|
177
|
-
- `Kind.of.Lambda()`
|
178
|
-
- `Kind.of.Boolean()`
|
179
|
-
|
180
|
-
#### Kind.is
|
181
|
-
|
182
|
-
- `Kind.of.Class()`
|
183
|
-
- `Kind.of.Module()`
|
184
|
-
- `Kind.of.Boolean()`
|
185
|
-
|
186
|
-
[⬆️ Back to Top](#table-of-contents-)
|
187
|
-
|
188
|
-
## How to create a new type checker?
|
152
|
+
### How to create a new type checker?
|
189
153
|
|
190
154
|
Use `Kind::Types.add()`. e.g:
|
191
155
|
|
@@ -222,7 +186,7 @@ Kind.of.User.class?(User) # true
|
|
222
186
|
|
223
187
|
[⬆️ Back to Top](#table-of-contents-)
|
224
188
|
|
225
|
-
|
189
|
+
#### What happens if a custom type checker has a namespace?
|
226
190
|
|
227
191
|
The type checker will preserve the namespace. ;)
|
228
192
|
|
@@ -268,6 +232,48 @@ Kind.of.Account::User::Membership.class?(Hash) # false
|
|
268
232
|
Kind.of.Account::User::Membership.class?(User) # true
|
269
233
|
```
|
270
234
|
|
235
|
+
[⬆️ Back to Top](#table-of-contents-)
|
236
|
+
|
237
|
+
## Built-in type checkers
|
238
|
+
|
239
|
+
The list of types (classes and modules) available to use with `Kind.of.*` or `Kind.is.*` are:
|
240
|
+
|
241
|
+
| Classes | Modules |
|
242
|
+
| ---------- | ---------- |
|
243
|
+
| String | Enumerable |
|
244
|
+
| Symbol | Comparable |
|
245
|
+
| Numeric | |
|
246
|
+
| Integer | |
|
247
|
+
| Float | |
|
248
|
+
| Regexp | |
|
249
|
+
| Time | |
|
250
|
+
| Array | |
|
251
|
+
| Range | |
|
252
|
+
| Hash | |
|
253
|
+
| Struct | |
|
254
|
+
| Enumerator | |
|
255
|
+
| Method | |
|
256
|
+
| Proc | |
|
257
|
+
| IO | |
|
258
|
+
| File | |
|
259
|
+
|
260
|
+
### Special type checkers
|
261
|
+
|
262
|
+
#### Kind.of
|
263
|
+
|
264
|
+
- `Kind.of.Class()`
|
265
|
+
- `Kind.of.Module()`
|
266
|
+
- `Kind.of.Lambda()`
|
267
|
+
- `Kind.of.Boolean()`
|
268
|
+
|
269
|
+
#### Kind.is
|
270
|
+
|
271
|
+
- `Kind.of.Class()`
|
272
|
+
- `Kind.of.Module()`
|
273
|
+
- `Kind.of.Boolean()`
|
274
|
+
|
275
|
+
[⬆️ Back to Top](#table-of-contents-)
|
276
|
+
|
271
277
|
## Development
|
272
278
|
|
273
279
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/kind.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'kind/version'
|
4
4
|
|
5
5
|
module Kind
|
6
|
+
Undefined = Object.new
|
7
|
+
|
6
8
|
class Error < TypeError
|
7
9
|
def initialize(klass, object)
|
8
10
|
super("#{object.inspect} expected to be a kind of #{klass}")
|
@@ -47,8 +49,8 @@ module Kind
|
|
47
49
|
raise Kind::Error.new((name || klass.name), object)
|
48
50
|
end
|
49
51
|
|
50
|
-
def self.Class(object =
|
51
|
-
return Class if object
|
52
|
+
def self.Class(object = Undefined)
|
53
|
+
return Class if object == Undefined
|
52
54
|
|
53
55
|
self.call(::Class, object)
|
54
56
|
end
|
@@ -63,8 +65,8 @@ module Kind
|
|
63
65
|
def self.instance?(value); class?(value); end
|
64
66
|
end)
|
65
67
|
|
66
|
-
def self.Module(object =
|
67
|
-
return Module if object
|
68
|
+
def self.Module(object = Undefined)
|
69
|
+
return Module if object == Undefined
|
68
70
|
|
69
71
|
self.call(::Module, object)
|
70
72
|
end
|
@@ -86,18 +88,18 @@ module Kind
|
|
86
88
|
COLONS = '::'.freeze
|
87
89
|
|
88
90
|
KIND_OF = <<-RUBY
|
89
|
-
def self.%{method_name}(object =
|
91
|
+
def self.%{method_name}(object = Undefined, options = {})
|
90
92
|
default = options[:or]
|
91
93
|
|
92
|
-
return Kind::Of::%{kind_name} if object
|
94
|
+
return Kind::Of::%{kind_name} if object == Undefined && default.nil?
|
93
95
|
|
94
96
|
Kind::Of.(::%{kind_name}, (object || default), name: "%{kind_name}".freeze)
|
95
97
|
end
|
96
98
|
RUBY
|
97
99
|
|
98
100
|
KIND_IS = <<-RUBY
|
99
|
-
def self.%{method_name}(value =
|
100
|
-
return Kind::Is::%{kind_name} if value
|
101
|
+
def self.%{method_name}(value = Undefined)
|
102
|
+
return Kind::Is::%{kind_name} if value == Undefined
|
101
103
|
|
102
104
|
Kind::Is.(::%{kind_name}, value)
|
103
105
|
end
|
@@ -202,10 +204,10 @@ module Kind
|
|
202
204
|
module Of
|
203
205
|
# -- Boolean
|
204
206
|
|
205
|
-
def self.Boolean(object =
|
207
|
+
def self.Boolean(object = Undefined, options = {})
|
206
208
|
default = options[:or]
|
207
209
|
|
208
|
-
return Kind::Of::Boolean if object
|
210
|
+
return Kind::Of::Boolean if object == Undefined && default.nil?
|
209
211
|
|
210
212
|
bool = object.nil? ? default : object
|
211
213
|
|
@@ -228,10 +230,10 @@ module Kind
|
|
228
230
|
|
229
231
|
# -- Lambda
|
230
232
|
|
231
|
-
def self.Lambda(object =
|
233
|
+
def self.Lambda(object = Undefined, options = {})
|
232
234
|
default = options[:or]
|
233
235
|
|
234
|
-
return Kind::Of::Lambda if object
|
236
|
+
return Kind::Of::Lambda if object == Undefined && default.nil?
|
235
237
|
|
236
238
|
func = object || default
|
237
239
|
|
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.1.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-04-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Basic type system for Ruby (free of dependencies).
|
14
14
|
email:
|