shape_of 1.1.0 → 1.2.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 +41 -0
- metadata +1 -1
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
@@ -316,6 +316,47 @@ module ShapeOf
|
|
316
316
|
end
|
317
317
|
end
|
318
318
|
|
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
|
+
|
319
360
|
Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
|
320
361
|
this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
|
321
362
|
end
|