shape_of 0.1.0 → 0.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 -4
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 200845fd006ae0119d06f0b1f3f8b7b908a7c3d8f8c16ebe4856bcb905af5763
4
- data.tar.gz: f046ee908eb3715e8028484dac72fa0fa411f24496ad3d71d8c984cf1a33fd7b
3
+ metadata.gz: 9663853638d239d448381e9ab748aeb7813a76b42ed9bd97c423f6d142c95d80
4
+ data.tar.gz: b1bdfeb51c08760c7c83519ce640c48d1202d23f3878adb93a2824761525219e
5
5
  SHA512:
6
- metadata.gz: f9d8e53319be78d4f3cf0aa6cc94f19f89c25decc737fecd1d16ce6e4666f35b80bca6ee9aff753ada64e85aa9d8c829cb4af06d67b33c114033c6f57838258a
7
- data.tar.gz: d70f5abeb46bd372329189755f901f5d56fa0e9d7d07d3a6724404719844c08ae4792042e6eaf417051e0bf33183f66f23fe29369003eefc0d6f7879157f9756
6
+ metadata.gz: 49cc6be2be276d554b25affe85bd97aca4ff3a134ee2a6334a0d37f35982f34ff28c39d3ce090418d2843b15dbc9d4b5b3bcbe568be69acbed90e31bcfab1ec6
7
+ data.tar.gz: cbaa2ed60315ffc111228a0f2093e950ad930a5ecbbba25c471f2d55044d1f14829e040e04d6e6705fcb767519994b1df46b67d0e61ea1046d90a54b87ff3057
data/lib/shape_of.rb CHANGED
@@ -244,10 +244,6 @@ module ShapeOf
244
244
 
245
245
  # Union[Shape1, Shape2, ...] denotes that it can be of one the provided shapes
246
246
  class Union < Shape
247
- def self.shape_of?(object)
248
- false
249
- end
250
-
251
247
  def self.[](*shapes)
252
248
  Class.new(self) do
253
249
  @class_name = "#{superclass.name}[#{shapes.map(&:inspect).join(", ")}]"
@@ -320,6 +316,47 @@ module ShapeOf
320
316
  end
321
317
  end
322
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
+
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
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: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Isom