shape_of 0.0.4 → 0.1.0
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 +14 -19
- 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: 200845fd006ae0119d06f0b1f3f8b7b908a7c3d8f8c16ebe4856bcb905af5763
|
4
|
+
data.tar.gz: f046ee908eb3715e8028484dac72fa0fa411f24496ad3d71d8c984cf1a33fd7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9d8e53319be78d4f3cf0aa6cc94f19f89c25decc737fecd1d16ce6e4666f35b80bca6ee9aff753ada64e85aa9d8c829cb4af06d67b33c114033c6f57838258a
|
7
|
+
data.tar.gz: d70f5abeb46bd372329189755f901f5d56fa0e9d7d07d3a6724404719844c08ae4792042e6eaf417051e0bf33183f66f23fe29369003eefc0d6f7879157f9756
|
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
|
-
|
92
|
+
assert_operator shape, :shape_of?, object
|
93
93
|
elsif shape.instance_of? ::Array
|
94
|
-
|
94
|
+
assert_operator Array[shape.first], :shape_of?, object
|
95
95
|
elsif shape.instance_of? ::Hash
|
96
|
-
|
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
|
-
|
104
|
+
refute_operator shape, :shape_of?, object
|
105
105
|
elsif shape.instance_of? ::Array
|
106
|
-
|
106
|
+
refute_operator Array[shape.first], :shape_of?, object
|
107
107
|
elsif shape.instance_of? ::Hash
|
108
|
-
|
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
|
@@ -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.shape_of?(object)
|
249
248
|
false
|
@@ -285,8 +284,7 @@ module ShapeOf
|
|
285
284
|
end
|
286
285
|
end
|
287
286
|
|
288
|
-
# Optional[
|
289
|
-
# (meaning if it is nil or the key is not present in the Hash, it's still true)
|
287
|
+
# 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)
|
290
288
|
class Optional < Shape
|
291
289
|
def self.[](shape)
|
292
290
|
raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
|
@@ -305,14 +303,13 @@ module ShapeOf
|
|
305
303
|
end
|
306
304
|
end
|
307
305
|
|
308
|
-
# Anything matches unless key does not exist in the Hash.
|
309
306
|
class Any < Shape
|
310
307
|
def self.shape_of?(object)
|
311
308
|
true
|
312
309
|
end
|
313
310
|
end
|
314
311
|
|
315
|
-
#
|
312
|
+
# Nothing only passes when the key does not exist in the Hash.
|
316
313
|
class Nothing < Shape
|
317
314
|
def self.shape_of?(object)
|
318
315
|
false
|
@@ -323,12 +320,10 @@ module ShapeOf
|
|
323
320
|
end
|
324
321
|
end
|
325
322
|
|
326
|
-
# Union[Integer, Float, Rational, Complex]
|
327
323
|
Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
|
328
324
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
|
329
325
|
end
|
330
326
|
|
331
|
-
# Union[TrueClass, FalseClass]
|
332
327
|
Boolean = Union[TrueClass, FalseClass].tap do |this|
|
333
328
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
|
334
329
|
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.0
|
4
|
+
version: 0.1.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
|