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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shape_of.rb +41 -0
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f32c3530c617efc49481f9e952d30996e62b429f591f28bad0b9c9bfaa161da
4
- data.tar.gz: b33c421e8bfac97b74e58970a7cacd6eb4ccc55c60637a5410ba4f688286a1a6
3
+ metadata.gz: 6e28929cd043b2227c3fcfc7ec697afeebdcd77aa4fe82f3239c0c70518a653b
4
+ data.tar.gz: ea1433d60b53e1cf9793e5824a02d08d990d079ab67d62684dbf5a9a8278227c
5
5
  SHA512:
6
- metadata.gz: 0bc0af5a46a7a8285ecebd29f76c2146032043346cc4604750835521f885062662732129fb807ea7059d815d9f90571abab52d3b84764a5886268303f1ed9882
7
- data.tar.gz: d18852f3ba98bfde393ad39466d4860b0c6eff6aa643d16fb237dba4170b1c46a5fd11abcb7cbbe3450376e7f8ba4799f075b9339c6dfeab8f70cc689189494e
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shape_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Isom