thy 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +15 -0
- data/lib/thy.rb +4 -0
- data/lib/thy/types.rb +8 -0
- data/lib/thy/types/class_extending.rb +25 -0
- data/lib/thy/types/instance_of.rb +19 -0
- data/lib/thy/types/nil.rb +15 -0
- data/thy.gemspec +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8e119f100c2d79df5089c32fc407f038e208ef8acdba30c01573b262a4eb139
|
4
|
+
data.tar.gz: 3d6000c7313dfbfe61fc1c1959a38c08ff3118b2b560d4f73cddf3c0c130c8f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5566cd48c42f865fdb7f2c328e4f98e723f82f69ba2a71150d765c144fe2209e68cf62f143c34bc657f773a8499d888495698f5c1517c4a146db5592286f3a89
|
7
|
+
data.tar.gz: aae23e9108b6e06aab5c0e3f71d4785673dce4c7e314bf913201cc9d1cf9c7621359692845172b1c80fc797773bd77c4e8cbb10dd27aad3e67969d32b702b397
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -126,6 +126,10 @@ Thy::Integer.check(3).success? # => true
|
|
126
126
|
# Boolean
|
127
127
|
Thy::Boolean.check(true).success? # => true
|
128
128
|
Thy::Boolean.check(false).success? # => true
|
129
|
+
|
130
|
+
# nil
|
131
|
+
Thy::Nil.check(nil).success? # => true
|
132
|
+
Thy::Nil.check(0).success? # => false
|
129
133
|
```
|
130
134
|
|
131
135
|
### Parameterized types
|
@@ -154,6 +158,17 @@ Thy::Variant('USD', 'EUR').check('EUR').success? # => true
|
|
154
158
|
# Option
|
155
159
|
Thy::Option(Thy::String).check('a string').success? # => true
|
156
160
|
Thy::Option(Thy::String).check(nil).success? # => true
|
161
|
+
|
162
|
+
# InstanceOf
|
163
|
+
class A; end
|
164
|
+
class B < A; end
|
165
|
+
class C; end
|
166
|
+
Thy::InstanceOf(A).check(B.new).success? # => true
|
167
|
+
Thy::InstanceOf(A).check(C.new).success? # => false
|
168
|
+
|
169
|
+
# ClassExtending
|
170
|
+
Thy::ClassExtending(A).check(B).success? # => true
|
171
|
+
Thy::ClassExtending(A).check(C).success? # => false
|
157
172
|
```
|
158
173
|
|
159
174
|
### Other types
|
data/lib/thy.rb
CHANGED
@@ -9,6 +9,7 @@ require 'thy/types/numeric'
|
|
9
9
|
require 'thy/types/integer'
|
10
10
|
require 'thy/types/float'
|
11
11
|
require 'thy/types/boolean'
|
12
|
+
require 'thy/types/nil'
|
12
13
|
|
13
14
|
require 'thy/types/array'
|
14
15
|
require 'thy/types/untyped_array'
|
@@ -22,6 +23,9 @@ require 'thy/types/variant'
|
|
22
23
|
|
23
24
|
require 'thy/types/option'
|
24
25
|
|
26
|
+
require 'thy/types/instance_of'
|
27
|
+
require 'thy/types/class_extending'
|
28
|
+
|
25
29
|
require 'thy/result/success'
|
26
30
|
require 'thy/result/failure'
|
27
31
|
|
data/lib/thy/types.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thy
|
4
|
+
module Types
|
5
|
+
class ClassExtending
|
6
|
+
def initialize(klass)
|
7
|
+
@klass = klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def check(value)
|
11
|
+
unless value.is_a?(::Class)
|
12
|
+
return Failure.new("Expected #{value.inspect} to be a Class")
|
13
|
+
end
|
14
|
+
|
15
|
+
if value.ancestors.include?(@klass)
|
16
|
+
Result::Success
|
17
|
+
else
|
18
|
+
Result::Failure.new(
|
19
|
+
"Expected #{value.inspect} to be a descendant of: #{@klass.inspect}",
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thy
|
4
|
+
module Types
|
5
|
+
class InstanceOf
|
6
|
+
def initialize(klass)
|
7
|
+
@klass = klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def check(value)
|
11
|
+
if value.is_a?(@klass)
|
12
|
+
Result::Success
|
13
|
+
else
|
14
|
+
Result::Failure.new("Expected #{value.inspect} to be an instance of: #{@klass.inspect}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/thy.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Komarov <ak@akxcv.com>
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,11 +105,14 @@ files:
|
|
105
105
|
- lib/thy/types.rb
|
106
106
|
- lib/thy/types/array.rb
|
107
107
|
- lib/thy/types/boolean.rb
|
108
|
+
- lib/thy/types/class_extending.rb
|
108
109
|
- lib/thy/types/enum.rb
|
109
110
|
- lib/thy/types/float.rb
|
110
111
|
- lib/thy/types/hash.rb
|
112
|
+
- lib/thy/types/instance_of.rb
|
111
113
|
- lib/thy/types/integer.rb
|
112
114
|
- lib/thy/types/map.rb
|
115
|
+
- lib/thy/types/nil.rb
|
113
116
|
- lib/thy/types/numeric.rb
|
114
117
|
- lib/thy/types/option.rb
|
115
118
|
- lib/thy/types/string.rb
|