kind 0.4.0 → 0.5.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 -1
- data/lib/kind.rb +22 -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: 381c55689c9c4b8077f859e76370db5f7c5f17efabdb83fb2902a2e33d602c42
|
4
|
+
data.tar.gz: de00093039551f1b6020a9d3a6cac31890f8fa7edfa1af620d4e9b3af1264749
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc6b4078e770ab68a50b7435f6b9a174dbe555b47ba64e0cf1e523e3b3bd1a1e936aa7368251a7b9be7b9203a48d6ab37d415d908c2cfcca49311a9cf0d425a7
|
7
|
+
data.tar.gz: b3d64254b98c33836bc903132fb4ba7dd75fd501fb6ad9bc14f1d4ab3a92e45d95dc4c81ea7f91559d2e43f110c9c7ad7650394d0bed7a585d04d70b876685df
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
![Ruby](https://img.shields.io/badge/ruby-2.2+-ruby.svg?colorA=99004d&colorB=cc0066)
|
1
2
|
[![Gem](https://img.shields.io/gem/v/kind.svg?style=flat-square)](https://rubygems.org/gems/kind)
|
2
3
|
[![Build Status](https://travis-ci.com/serradura/kind.svg?branch=master)](https://travis-ci.com/serradura/kind)
|
3
4
|
|
@@ -12,6 +13,7 @@ As a creator of Ruby gems, I have a common need that I have to handle in many of
|
|
12
13
|
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 to you check out the [dry-types gem](https://dry-rb.org/gems/dry-types).
|
13
14
|
|
14
15
|
## Table of Contents <!-- omit in toc -->
|
16
|
+
- [Required Ruby version](#required-ruby-version)
|
15
17
|
- [Installation](#installation)
|
16
18
|
- [Usage](#usage)
|
17
19
|
- [Verifying the kind of some object](#verifying-the-kind-of-some-object)
|
@@ -26,6 +28,9 @@ One of the goals of this project is to do simple type checking like `"some strin
|
|
26
28
|
- [License](#license)
|
27
29
|
- [Code of Conduct](#code-of-conduct)
|
28
30
|
|
31
|
+
## Required Ruby version
|
32
|
+
> \>= 2.2.0
|
33
|
+
|
29
34
|
## Installation
|
30
35
|
|
31
36
|
Add this line to your application's Gemfile:
|
@@ -48,7 +53,7 @@ Or install it yourself as:
|
|
48
53
|
|
49
54
|
### Verifying the kind of some object
|
50
55
|
|
51
|
-
By default, basic
|
56
|
+
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.
|
52
57
|
|
53
58
|
```ruby
|
54
59
|
Kind.of.Hash('')
|
@@ -66,6 +71,20 @@ Kind.of.Boolean(true) # true
|
|
66
71
|
Kind.of.Boolean(false) # false
|
67
72
|
```
|
68
73
|
|
74
|
+
When the verified value is nil, it is possible to define a default value with the same type to be returned.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
value = nil
|
78
|
+
|
79
|
+
Kind.of.Hash(value, or: {})
|
80
|
+
# {}
|
81
|
+
|
82
|
+
# ---
|
83
|
+
|
84
|
+
Kind.of.Boolean(nil, or: true)
|
85
|
+
# true
|
86
|
+
```
|
87
|
+
|
69
88
|
As an alternative syntax, you can use the `Kind::Of` instead of the method. e.g: `Kind::Of::Hash('')`
|
70
89
|
|
71
90
|
But if you don't need a strict type verification, use the `.or_nil`method
|
data/lib/kind.rb
CHANGED
@@ -86,10 +86,12 @@ module Kind
|
|
86
86
|
extend self
|
87
87
|
|
88
88
|
KIND_OF = <<-RUBY
|
89
|
-
def self.%{name}(object = nil)
|
90
|
-
|
89
|
+
def self.%{name}(object = nil, options = {})
|
90
|
+
default = options[:or]
|
91
91
|
|
92
|
-
Kind::Of
|
92
|
+
return Kind::Of::%{name} if object.nil? && default.nil?
|
93
|
+
|
94
|
+
Kind::Of.call(::%{name}, object || default)
|
93
95
|
end
|
94
96
|
RUBY
|
95
97
|
|
@@ -143,12 +145,16 @@ module Kind
|
|
143
145
|
module Of
|
144
146
|
# -- Boolean
|
145
147
|
|
146
|
-
def self.Boolean(object = nil)
|
147
|
-
|
148
|
+
def self.Boolean(object = nil, options = {})
|
149
|
+
default = options[:or]
|
150
|
+
|
151
|
+
return Kind::Of::Boolean if object.nil? && default.nil?
|
152
|
+
|
153
|
+
bool = object.nil? ? default : object
|
148
154
|
|
149
|
-
return
|
155
|
+
return bool if bool.is_a?(::TrueClass) || bool.is_a?(::FalseClass)
|
150
156
|
|
151
|
-
raise Kind::Error.new('Boolean'.freeze,
|
157
|
+
raise Kind::Error.new('Boolean'.freeze, bool)
|
152
158
|
end
|
153
159
|
|
154
160
|
const_set(:Boolean, ::Class.new(Checker) do
|
@@ -163,17 +169,21 @@ module Kind
|
|
163
169
|
|
164
170
|
# -- Lambda
|
165
171
|
|
166
|
-
def self.Lambda(object = nil)
|
167
|
-
|
172
|
+
def self.Lambda(object = nil, options = {})
|
173
|
+
default = options[:or]
|
174
|
+
|
175
|
+
return Kind::Of::Lambda if object.nil? && default.nil?
|
176
|
+
|
177
|
+
func = object || default
|
168
178
|
|
169
|
-
return
|
179
|
+
return func if func.is_a?(::Proc) && func.lambda?
|
170
180
|
|
171
|
-
raise Kind::Error.new('Lambda'.freeze,
|
181
|
+
raise Kind::Error.new('Lambda'.freeze, func)
|
172
182
|
end
|
173
183
|
|
174
184
|
const_set(:Lambda, ::Class.new(Checker) do
|
175
185
|
def instance?(value)
|
176
|
-
value.is_a?(
|
186
|
+
value.is_a?(@type) && value.lambda?
|
177
187
|
end
|
178
188
|
end.new(::Proc).freeze)
|
179
189
|
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: 0.
|
4
|
+
version: 0.5.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-01-
|
11
|
+
date: 2020-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Basic type system for Ruby (free of dependencies).
|
14
14
|
email:
|