shape_of 1.0.0 → 2.0.1

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shape_of.rb +37 -8
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6029b808200bb28201b18af2fac69cdd6c7ef68a27c55d7c94d95ee459dcdc3e
4
- data.tar.gz: 7a07a494f474b3ed1fb1f33d0f6f6633b5e2c1a27b69abe3f37cbcba7c60cb86
3
+ metadata.gz: 64fc24c8d36d335c6052f39457aac2f62dd4155538e57dda11c78e080748bac7
4
+ data.tar.gz: ff28778300c41c9352a83da9d6ae2b95c75150dd5429abfd5e3a90c0dd1397e5
5
5
  SHA512:
6
- metadata.gz: 1874ea9da0b26864298dd7b9ccd5874fa39301c05c83e83ec18d4943726c6a58c877bed7e2fdafb5ad64cdbb40afe3a5611673e591a97315265644cd05741b8c
7
- data.tar.gz: b9c36bdcc7fb8b5ef7f5913e7ec5fd34cbfc9dbffc5e8bd7a058f54f0ace2adafa9ed937a710a5780a3755b785a456d571af4bd67140d095d0de8c4d3d8df989
6
+ metadata.gz: 0725a89f09c62099a96287fdc2c14080d004de6f1682e9cf09cefc6ed7300e535cfb7d375f7e913d8ccd22b5f2b23b52da8072aa85dbfe2cca3dc7d6480672b5
7
+ data.tar.gz: e285973c38a51f6fcbb662e82322ea9bcb291388617849e0b86bdfd0e44cf2c4007ae456f5259fffef63f80d178c3f94016b49fa4ef2096c4d39a2e7992ae84d
data/lib/shape_of.rb CHANGED
@@ -89,11 +89,11 @@ module ShapeOf
89
89
  module Assertions
90
90
  def assert_shape_of(object, shape)
91
91
  if shape.respond_to? :shape_of?
92
- assert shape.shape_of? object
92
+ assert_operator shape, :shape_of?, object
93
93
  elsif shape.instance_of? ::Array
94
- assert Array[shape.first].shape_of? object
94
+ assert_operator Array[shape.first], :shape_of?, object
95
95
  elsif shape.instance_of? ::Hash
96
- assert Hash[shape].shape_of? object
96
+ assert_operator Hash[shape], :shape_of?, object
97
97
  else
98
98
  raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
99
99
  end
@@ -101,11 +101,11 @@ module ShapeOf
101
101
 
102
102
  def refute_shape_of(object, shape)
103
103
  if shape.respond_to? :shape_of?
104
- refute shape.shape_of? object
104
+ refute_operator shape, :shape_of?, object
105
105
  elsif shape.instance_of? ::Array
106
- refute Array[shape.first].shape_of? object
106
+ refute_operator Array[shape.first], :shape_of?, object
107
107
  elsif shape.instance_of? ::Hash
108
- refute Hash[shape].shape_of? object
108
+ refute_operator Hash[shape], :shape_of?, object
109
109
  else
110
110
  raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
111
111
  end
@@ -161,7 +161,7 @@ module ShapeOf
161
161
  if @shape.respond_to? :shape_of?
162
162
  @shape.shape_of? elem
163
163
  elsif @shape.is_a? ::Array
164
- Array[@shape].shape_of? elem
164
+ Array[@shape.first].shape_of? elem
165
165
  elsif @shape.is_a? ::Hash
166
166
  Hash[@shape].shape_of? elem
167
167
  elsif @shape.is_a? Class
@@ -222,7 +222,7 @@ module ShapeOf
222
222
  if @shape[key].respond_to? :shape_of?
223
223
  @shape[key].shape_of? elem
224
224
  elsif @shape[key].is_a? ::Array
225
- Array[@shape[key]].shape_of? elem
225
+ Array[@shape[key].first].shape_of? elem
226
226
  elsif @shape[key].is_a? ::Hash
227
227
  Hash[@shape[key]].shape_of? elem
228
228
  elsif @shape[key].is_a? Class
@@ -316,6 +316,35 @@ module ShapeOf
316
316
  end
317
317
  end
318
318
 
319
+ class Pattern < Shape
320
+ def self.[](shape)
321
+ raise TypeError, "Shape must be #{Regexp.inspect}, was #{shape.inspect}" unless shape.instance_of? Regexp
322
+
323
+ Class.new(self) do
324
+ @class_name = "#{superclass.name}[#{shape.inspect}]"
325
+ @shape = shape
326
+
327
+ def self.name
328
+ @class_name
329
+ end
330
+
331
+ def self.to_s
332
+ @class_name
333
+ end
334
+
335
+ def self.inspect
336
+ @class_name
337
+ end
338
+
339
+ def self.shape_of?(object)
340
+ raise TypeError, "expected #{String.inspect}, was instead #{object.inspect}" unless object.instance_of?(String)
341
+
342
+ @shape.match?(object)
343
+ end
344
+ end
345
+ end
346
+ end
347
+
319
348
  Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
320
349
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
321
350
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shape_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Isom
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-15 00:00:00.000000000 Z
11
+ date: 2021-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest