shape_of 0.1.0 → 0.2.2
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/lib/shape_of.rb +45 -12
- 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: 752eb59a030bfd813d4d4a06fb470860dcc4906566601b606b31ae6d2e9a377e
|
4
|
+
data.tar.gz: 8549d7a936ead7069f6a2976045b1b9a2a41514dd2aa87086a0009c030c1b2cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efe740174b7c8f1c4ede6040e118038cd97e6f7e6b3e21b19e5d97d947bfbe2aaf4399b9b3b905893735eeaeb5b1277ec4188980a5a2874468fd989068a4fbea
|
7
|
+
data.tar.gz: 9bef6f76ff9fcb92ae39c1fa88c3cd608d843cff8ce189de92651a77ee92aba7343dfe2459e7d79f75167508a46a8ebea55d1b60aaa2169940a340428378b03b
|
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 Hash, 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].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
|
@@ -175,7 +175,8 @@ module ShapeOf
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
178
|
-
# Hash[key:
|
178
|
+
# Hash[key: shape, ...] denotes it is a hash of shapes with a very specific structure.
|
179
|
+
# Hash (without square brackets) is just a hash with any shape.
|
179
180
|
# This, along with Array, are the core components of this module.
|
180
181
|
# Note that the keys are converted to strings for comparison for both the shape and object provided.
|
181
182
|
class Hash < Shape
|
@@ -222,7 +223,7 @@ module ShapeOf
|
|
222
223
|
if @shape[key].respond_to? :shape_of?
|
223
224
|
@shape[key].shape_of? elem
|
224
225
|
elsif @shape[key].is_a? ::Array
|
225
|
-
Array[@shape[key]].shape_of? elem
|
226
|
+
Array[@shape[key].first].shape_of? elem
|
226
227
|
elsif @shape[key].is_a? ::Hash
|
227
228
|
Hash[@shape[key]].shape_of? elem
|
228
229
|
elsif @shape[key].is_a? Class
|
@@ -242,12 +243,8 @@ module ShapeOf
|
|
242
243
|
end
|
243
244
|
end
|
244
245
|
|
245
|
-
# Union[
|
246
|
+
# Union[shape1, shape2, ...] denotes that it can be of one the provided shapes.
|
246
247
|
class Union < Shape
|
247
|
-
def self.shape_of?(object)
|
248
|
-
false
|
249
|
-
end
|
250
|
-
|
251
248
|
def self.[](*shapes)
|
252
249
|
Class.new(self) do
|
253
250
|
@class_name = "#{superclass.name}[#{shapes.map(&:inspect).join(", ")}]"
|
@@ -284,7 +281,8 @@ module ShapeOf
|
|
284
281
|
end
|
285
282
|
end
|
286
283
|
|
287
|
-
# Optional[
|
284
|
+
# Optional[shape] denotes that the usual type is a shape, but is optional
|
285
|
+
# (meaning if it is nil or the key is not present in the Hash, it's still true)
|
288
286
|
class Optional < Shape
|
289
287
|
def self.[](shape)
|
290
288
|
raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
|
@@ -303,13 +301,14 @@ module ShapeOf
|
|
303
301
|
end
|
304
302
|
end
|
305
303
|
|
304
|
+
# Anything matches unless key does not exist in the Hash.
|
306
305
|
class Any < Shape
|
307
306
|
def self.shape_of?(object)
|
308
307
|
true
|
309
308
|
end
|
310
309
|
end
|
311
310
|
|
312
|
-
#
|
311
|
+
# Only passes when the key does not exist in the Hash.
|
313
312
|
class Nothing < Shape
|
314
313
|
def self.shape_of?(object)
|
315
314
|
false
|
@@ -320,10 +319,44 @@ module ShapeOf
|
|
320
319
|
end
|
321
320
|
end
|
322
321
|
|
322
|
+
# Matches a Regexp against a String using Regexp#match?.
|
323
|
+
# Pretty much a wrapper around Regexp because a Regexp instance will be tested for equality
|
324
|
+
# in the ShapeOf::Hash and ShapeOf::Array since it's not a class.
|
325
|
+
class Pattern < Shape
|
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
|
+
raise TypeError, "expected #{String.inspect}, was instead #{object.inspect}" unless object.instance_of?(String)
|
347
|
+
|
348
|
+
@shape.match?(object)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# Union[Integer, Float, Rational, Complex]
|
323
355
|
Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
|
324
356
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
|
325
357
|
end
|
326
358
|
|
359
|
+
# Union[TrueClass, FalseClass]
|
327
360
|
Boolean = Union[TrueClass, FalseClass].tap do |this|
|
328
361
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
|
329
362
|
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: 0.
|
4
|
+
version: 0.2.2
|
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-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|