shape_of 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shape_of.rb +49 -17
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f553350ffd20cf6d41ccdedd05e1270d9af3e7537888620d59b245bfd1a7a46d
4
- data.tar.gz: 8478b7134ffb95be65729e25083c7bc761787bf62c5601b2707c80b417418bfd
3
+ metadata.gz: 9663853638d239d448381e9ab748aeb7813a76b42ed9bd97c423f6d142c95d80
4
+ data.tar.gz: b1bdfeb51c08760c7c83519ce640c48d1202d23f3878adb93a2824761525219e
5
5
  SHA512:
6
- metadata.gz: 773d54bc36873a4890a7c71d90a30f1610b924fcac074350952a4e8b08ad3ba5702edc4a4d32eb640edc99fd8f233a8e7dc959aa7824633bd4286fb9f65cf71c
7
- data.tar.gz: 1ac8506767c31db60322b1395bbf1cc58ef7854483b24e8aba6e260e482fa2ca49406553fa93ca6cc79911d1e0939a56dcfe3a73a38ce2c7c406eb47c076a32e
6
+ metadata.gz: 49cc6be2be276d554b25affe85bd97aca4ff3a134ee2a6334a0d37f35982f34ff28c39d3ce090418d2843b15dbc9d4b5b3bcbe568be69acbed90e31bcfab1ec6
7
+ data.tar.gz: cbaa2ed60315ffc111228a0f2093e950ad930a5ecbbba25c471f2d55044d1f14829e040e04d6e6705fcb767519994b1df46b67d0e61ea1046d90a54b87ff3057
data/lib/shape_of.rb CHANGED
@@ -127,9 +127,9 @@ module ShapeOf
127
127
  end
128
128
  end
129
129
 
130
- # Array[shape] denotes that it is an array of shapes.
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 Hash, are the core components of this module.
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.first].shape_of? elem
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: 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.
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].first].shape_of? elem
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,12 +242,8 @@ module ShapeOf
243
242
  end
244
243
  end
245
244
 
246
- # Union[shape1, shape2, ...] denotes that it can be of one the provided shapes.
245
+ # Union[Shape1, Shape2, ...] denotes that it can be of one the provided shapes
247
246
  class Union < Shape
248
- def self.shape_of?(object)
249
- false
250
- end
251
-
252
247
  def self.[](*shapes)
253
248
  Class.new(self) do
254
249
  @class_name = "#{superclass.name}[#{shapes.map(&:inspect).join(", ")}]"
@@ -285,8 +280,7 @@ module ShapeOf
285
280
  end
286
281
  end
287
282
 
288
- # Optional[shape] denotes that the usual type is a shape, but is optional
289
- # (meaning if it is nil or the key is not present in the Hash, it's still true)
283
+ # 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
284
  class Optional < Shape
291
285
  def self.[](shape)
292
286
  raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
@@ -305,14 +299,13 @@ module ShapeOf
305
299
  end
306
300
  end
307
301
 
308
- # Anything matches unless key does not exist in the Hash.
309
302
  class Any < Shape
310
303
  def self.shape_of?(object)
311
304
  true
312
305
  end
313
306
  end
314
307
 
315
- # Only passes when the key does not exist in the Hash.
308
+ # Nothing only passes when the key does not exist in the Hash.
316
309
  class Nothing < Shape
317
310
  def self.shape_of?(object)
318
311
  false
@@ -323,12 +316,51 @@ module ShapeOf
323
316
  end
324
317
  end
325
318
 
326
- # Union[Integer, Float, Rational, Complex]
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
+
327
360
  Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
328
361
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
329
362
  end
330
363
 
331
- # Union[TrueClass, FalseClass]
332
364
  Boolean = Union[TrueClass, FalseClass].tap do |this|
333
365
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
334
366
  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.1.2
4
+ version: 0.2.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-28 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest