shape_of 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/shape_of.rb +49 -13
- 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: 6e28929cd043b2227c3fcfc7ec697afeebdcd77aa4fe82f3239c0c70518a653b
|
4
|
+
data.tar.gz: ea1433d60b53e1cf9793e5824a02d08d990d079ab67d62684dbf5a9a8278227c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f4a6d76316870ae8945faa6de5797647c9aa9cbe259017b7acaf773a75c6100dfea4770d1f290dd2e8abcd37293111b74a6beb60767db6814a4ffab7714fec5
|
7
|
+
data.tar.gz: 8dae349aab3980850618761687ad7b3e61701dd9e29ebdc6880757bb744dd5f708e5a63d931de5121777e619054c7cdddfd8931416cd40a5960448bd89c3e60e
|
data/lib/shape_of.rb
CHANGED
@@ -127,9 +127,9 @@ module ShapeOf
|
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
|
-
# Array[
|
130
|
+
# Array[Shape] denotes that it is an array of shapes.
|
131
131
|
# It checks every element in the array and verifies that the element is in the correct shape.
|
132
|
-
# This, along with
|
132
|
+
# This, along with Array, are the core components of this module.
|
133
133
|
# Note that a ShapeOf::Array[Integer].shape_of?([]) will pass because it is vacuously true for an empty array.
|
134
134
|
class Array < Shape
|
135
135
|
@internal_class = ::Array
|
@@ -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
|
164
|
+
Array[@shape].shape_of? elem
|
165
165
|
elsif @shape.is_a? ::Hash
|
166
166
|
Hash[@shape].shape_of? elem
|
167
167
|
elsif @shape.is_a? Class
|
@@ -175,8 +175,7 @@ module ShapeOf
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
178
|
-
# Hash[key:
|
179
|
-
# Hash (without square brackets) is just a hash with any shape.
|
178
|
+
# Hash[key: Shape, ...] denotes it is a hash of shapes with a very specific structure. Hash (without square brackets) is just a hash with any shape.
|
180
179
|
# This, along with Array, are the core components of this module.
|
181
180
|
# Note that the keys are converted to strings for comparison for both the shape and object provided.
|
182
181
|
class Hash < Shape
|
@@ -223,7 +222,7 @@ module ShapeOf
|
|
223
222
|
if @shape[key].respond_to? :shape_of?
|
224
223
|
@shape[key].shape_of? elem
|
225
224
|
elsif @shape[key].is_a? ::Array
|
226
|
-
Array[@shape[key]
|
225
|
+
Array[@shape[key]].shape_of? elem
|
227
226
|
elsif @shape[key].is_a? ::Hash
|
228
227
|
Hash[@shape[key]].shape_of? elem
|
229
228
|
elsif @shape[key].is_a? Class
|
@@ -243,7 +242,7 @@ module ShapeOf
|
|
243
242
|
end
|
244
243
|
end
|
245
244
|
|
246
|
-
# Union[
|
245
|
+
# Union[Shape1, Shape2, ...] denotes that it can be of one the provided shapes
|
247
246
|
class Union < Shape
|
248
247
|
def self.[](*shapes)
|
249
248
|
Class.new(self) do
|
@@ -281,8 +280,7 @@ module ShapeOf
|
|
281
280
|
end
|
282
281
|
end
|
283
282
|
|
284
|
-
# Optional[
|
285
|
-
# (meaning if it is nil or the key is not present in the Hash, it's still true)
|
283
|
+
# Optional[Shape] denotes that the usual type is a Shape, but is optional (meaning if it is nil or the key is not present in the Hash, it's still true)
|
286
284
|
class Optional < Shape
|
287
285
|
def self.[](shape)
|
288
286
|
raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
|
@@ -301,14 +299,13 @@ module ShapeOf
|
|
301
299
|
end
|
302
300
|
end
|
303
301
|
|
304
|
-
# Anything matches unless key does not exist in the Hash.
|
305
302
|
class Any < Shape
|
306
303
|
def self.shape_of?(object)
|
307
304
|
true
|
308
305
|
end
|
309
306
|
end
|
310
307
|
|
311
|
-
#
|
308
|
+
# Nothing only passes when the key does not exist in the Hash.
|
312
309
|
class Nothing < Shape
|
313
310
|
def self.shape_of?(object)
|
314
311
|
false
|
@@ -319,12 +316,51 @@ module ShapeOf
|
|
319
316
|
end
|
320
317
|
end
|
321
318
|
|
322
|
-
|
319
|
+
class Regexp < Shape
|
320
|
+
@internal_class = ::Regexp
|
321
|
+
|
322
|
+
def self.shape_of?(object)
|
323
|
+
object.instance_of? @internal_class
|
324
|
+
end
|
325
|
+
|
326
|
+
def self.[](shape)
|
327
|
+
raise TypeError, "Shape must be #{::Regexp.inspect}, was #{shape.inspect}" unless shape.instance_of? ::Regexp
|
328
|
+
|
329
|
+
Class.new(self) do
|
330
|
+
@class_name = "#{superclass.name}[#{shape.inspect}]"
|
331
|
+
@shape = shape
|
332
|
+
|
333
|
+
def self.name
|
334
|
+
@class_name
|
335
|
+
end
|
336
|
+
|
337
|
+
def self.to_s
|
338
|
+
@class_name
|
339
|
+
end
|
340
|
+
|
341
|
+
def self.inspect
|
342
|
+
@class_name
|
343
|
+
end
|
344
|
+
|
345
|
+
def self.shape_of?(object)
|
346
|
+
unless object.instance_of?(::Regexp) || object.instance_of?(String)
|
347
|
+
raise TypeError, "expected #{::Regexp.inspect} or #{String.inspect}, was instead #{object.inspect}"
|
348
|
+
end
|
349
|
+
|
350
|
+
if object.instance_of?(::Regexp)
|
351
|
+
@shape == object
|
352
|
+
else # string
|
353
|
+
@shape.match?(object)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
323
360
|
Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
|
324
361
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
|
325
362
|
end
|
326
363
|
|
327
|
-
# Union[TrueClass, FalseClass]
|
328
364
|
Boolean = Union[TrueClass, FalseClass].tap do |this|
|
329
365
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
|
330
366
|
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.
|
4
|
+
version: 1.2.0
|
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-
|
11
|
+
date: 2021-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|