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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d991b917ad68769931249608ff697e1ecb4e692cede48983f1a0dcb6d36bba0
4
- data.tar.gz: 5f74f8a6755537730bc4c483d228bd2d8417dbfff446b3b226d65a60f11b091f
3
+ metadata.gz: a8e119f100c2d79df5089c32fc407f038e208ef8acdba30c01573b262a4eb139
4
+ data.tar.gz: 3d6000c7313dfbfe61fc1c1959a38c08ff3118b2b560d4f73cddf3c0c130c8f4
5
5
  SHA512:
6
- metadata.gz: 49659a10e9612478b79d7aafe0337b3e8281bbf9044f181492fb37b2781dbeeab8a47a1af69b3524a88bdbade4f820fa6b59477dd9a9e0b71488908b371d326b
7
- data.tar.gz: 34583cc86c792b32fa07741f5213757ffcd7c5c2733d7008fc1b782681c16d788d459293a6f1c89a59c91bc8aa526310770e6787674ec7660f24a51154054a02
6
+ metadata.gz: 5566cd48c42f865fdb7f2c328e4f98e723f82f69ba2a71150d765c144fe2209e68cf62f143c34bc657f773a8499d888495698f5c1517c4a146db5592286f3a89
7
+ data.tar.gz: aae23e9108b6e06aab5c0e3f71d4785673dce4c7e314bf913201cc9d1cf9c7621359692845172b1c80fc797773bd77c4e8cbb10dd27aad3e67969d32b702b397
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /thy-*.gem
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- thy (0.1.2)
4
+ thy (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
 
@@ -30,6 +30,14 @@ module Thy::Types
30
30
  def Option(type)
31
31
  Option.new(type)
32
32
  end
33
+
34
+ def InstanceOf(klass)
35
+ InstanceOf.new(klass)
36
+ end
37
+
38
+ def ClassExtending(klass)
39
+ ClassExtending.new(klass)
40
+ end
33
41
  # rubocop:enable Naming/MethodName
34
42
  end
35
43
  end
@@ -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
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Nil
6
+ def self.check(value)
7
+ if value.nil?
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected nil, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'thy'
8
- spec.version = '0.1.2'
8
+ spec.version = '0.1.3'
9
9
  spec.authors = ['Alexander Komarov <ak@akxcv.com>']
10
10
  spec.email = ['ak@akxcv.com']
11
11
 
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.2
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-02 00:00:00.000000000 Z
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