shape_of 1.2.2 → 2.0.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 +8 -16
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee7f353196efa5fe84c5b077228696920b0cfecc74c0f7da62ef18eaa15e4cd5
4
- data.tar.gz: 4ae20130b4b1f2307755015f0bffcc6a82138ab6a75e81e8c71012e5c7926383
3
+ metadata.gz: 9934786c810465f0edc75a35faa47a305da950a63c337ec1c33b7184c1dd103e
4
+ data.tar.gz: bf6c05010803284ccb2a6be09d410d903d3693e53ec006be85089a1c0522257c
5
5
  SHA512:
6
- metadata.gz: a51dd81d6468c8cee82ad6a633eea7510950a475f8bd7d3bed251f88a4e029d7c5f6a557dc26e084706b17b4904a50485814814748c20aff4c1eb5de4fb3be66
7
- data.tar.gz: ebdd687b4046fbd6bce79024f927aee2ef0e625776c4bfde98a4b12d12513b66d5a64c7df4a7871c75e249358ae1142f4c787c10d1ee4e23584951fd45b1e372
6
+ metadata.gz: 19c075c5e72d649267aab15fcdf3872a104ac8ac2534e95159f121ca15cdd2add334f0010f53bd82b64c5ce09b1910717e04e7a6374febb86b143936d05afa4d
7
+ data.tar.gz: 4166c74b3c7df3ae858b2edeb1239c306e05479c96a0536b08815b279c82209f563f2f469a7ab6d1254c12f249d6014d484f12077e4a6471aa223ef0a15d2578
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,7 +242,7 @@ 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
247
  def self.[](*shapes)
249
248
  Class.new(self) do
@@ -281,8 +280,7 @@ module ShapeOf
281
280
  end
282
281
  end
283
282
 
284
- # Optional[shape] denotes that the usual type is a shape, but is optional
285
- # (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)
286
284
  class Optional < Shape
287
285
  def self.[](shape)
288
286
  raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
@@ -301,14 +299,13 @@ module ShapeOf
301
299
  end
302
300
  end
303
301
 
304
- # Anything matches unless key does not exist in the Hash.
305
302
  class Any < Shape
306
303
  def self.shape_of?(object)
307
304
  true
308
305
  end
309
306
  end
310
307
 
311
- # Only passes when the key does not exist in the Hash.
308
+ # Nothing only passes when the key does not exist in the Hash.
312
309
  class Nothing < Shape
313
310
  def self.shape_of?(object)
314
311
  false
@@ -319,9 +316,6 @@ module ShapeOf
319
316
  end
320
317
  end
321
318
 
322
- # Matches a Regexp against a String using Regexp#match?.
323
- # Pretty much a wrapper around Regexp because a Regexp instance will be tested for equality
324
- # in the ShapeOf::Hash and ShapeOf::Array since it's not a class.
325
319
  class Pattern < Shape
326
320
  def self.[](shape)
327
321
  raise TypeError, "Shape must be #{Regexp.inspect}, was #{shape.inspect}" unless shape.instance_of? Regexp
@@ -351,12 +345,10 @@ module ShapeOf
351
345
  end
352
346
  end
353
347
 
354
- # Union[Integer, Float, Rational, Complex]
355
348
  Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
356
349
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
357
350
  end
358
351
 
359
- # Union[TrueClass, FalseClass]
360
352
  Boolean = Union[TrueClass, FalseClass].tap do |this|
361
353
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
362
354
  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: 1.2.2
4
+ version: 2.0.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