kind 0.4.0 → 0.5.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -1
  3. data/lib/kind.rb +22 -12
  4. data/lib/kind/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db734da8b7d777098f049983dce40d7a0f36d6a6d9c61ade223275c5d023c22e
4
- data.tar.gz: 0e1dea80ac9fdebdf2af1e0a6becd1aa970294906e5f5f8cd7c8836a9c4dc5f5
3
+ metadata.gz: 381c55689c9c4b8077f859e76370db5f7c5f17efabdb83fb2902a2e33d602c42
4
+ data.tar.gz: de00093039551f1b6020a9d3a6cac31890f8fa7edfa1af620d4e9b3af1264749
5
5
  SHA512:
6
- metadata.gz: 96346a48789ccc9fcd7a0308eca390753bc3b8e397a048c7bb049929252214c7ba6d100ae17fb14a1e98595817120aa8c4900d57a4638bc8ee9494eb8682b259
7
- data.tar.gz: 07410f5ec41a04cfaaaf660f1ad933e238efb1b2845b7e11c2ba69602c31bd69517ece4d3056c1e62b4c89faa04824344cdd8634ba593470c48b227bc7a9e8c8
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 type verification is strict. So, when you perform `Kind.of.Hash(value)`, if the given value was a Hash it will be returned, but if it wasn't one, an error will be raised.
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
@@ -86,10 +86,12 @@ module Kind
86
86
  extend self
87
87
 
88
88
  KIND_OF = <<-RUBY
89
- def self.%{name}(object = nil)
90
- return Kind::Of::%{name} if object.nil?
89
+ def self.%{name}(object = nil, options = {})
90
+ default = options[:or]
91
91
 
92
- Kind::Of.call(::%{name}, object)
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
- return Kind::Of::Boolean if object.nil?
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 object if object.is_a?(::TrueClass) || object.is_a?(::FalseClass)
155
+ return bool if bool.is_a?(::TrueClass) || bool.is_a?(::FalseClass)
150
156
 
151
- raise Kind::Error.new('Boolean'.freeze, object)
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
- return Kind::Of::Lambda if object.nil?
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 object if object.is_a?(::Proc) && object.lambda?
179
+ return func if func.is_a?(::Proc) && func.lambda?
170
180
 
171
- raise Kind::Error.new('Lambda'.freeze, object)
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?(::Proc) && value.lambda?
186
+ value.is_a?(@type) && value.lambda?
177
187
  end
178
188
  end.new(::Proc).freeze)
179
189
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kind
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
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.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-03 00:00:00.000000000 Z
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: