shape_of 0.0.2 → 0.1.2

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 +19 -14
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d29d9baae8a0e917051667ba6d9526372191522dc82a75e06fc17171b8809526
4
- data.tar.gz: 0c242d2d00c3acf7f690b178760ca164f01b45c8d782145004ee3972d1cd25cd
3
+ metadata.gz: f553350ffd20cf6d41ccdedd05e1270d9af3e7537888620d59b245bfd1a7a46d
4
+ data.tar.gz: 8478b7134ffb95be65729e25083c7bc761787bf62c5601b2707c80b417418bfd
5
5
  SHA512:
6
- metadata.gz: 170be793850595786dddbd03fafa7244e68aa7a19aee1757bbe439b4169cc9ea2e612371231c9e331a61d814dfae94afa0149afd2d3f754b542a7e01b4b790e1
7
- data.tar.gz: 859512b4a374791f987296f27b774c5b9ac7fa5833505a3abff4ff72cc9942d6c0c8ff226daff3a7ea7dcef81fce80f31b3b3b1f7bc2d307e8491dae736dab67
6
+ metadata.gz: 773d54bc36873a4890a7c71d90a30f1610b924fcac074350952a4e8b08ad3ba5702edc4a4d32eb640edc99fd8f233a8e7dc959aa7824633bd4286fb9f65cf71c
7
+ data.tar.gz: 1ac8506767c31db60322b1395bbf1cc58ef7854483b24e8aba6e260e482fa2ca49406553fa93ca6cc79911d1e0939a56dcfe3a73a38ce2c7c406eb47c076a32e
data/lib/shape_of.rb CHANGED
@@ -89,11 +89,11 @@ module ShapeOf
89
89
  module Assertions
90
90
  def assert_shape_of(object, shape)
91
91
  if shape.respond_to? :shape_of?
92
- assert shape.shape_of? object
92
+ assert_operator shape, :shape_of?, object
93
93
  elsif shape.instance_of? ::Array
94
- assert Array[shape.first].shape_of? object
94
+ assert_operator Array[shape.first], :shape_of?, object
95
95
  elsif shape.instance_of? ::Hash
96
- assert Hash[shape].shape_of? object
96
+ assert_operator Hash[shape], :shape_of?, object
97
97
  else
98
98
  raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
99
99
  end
@@ -101,11 +101,11 @@ module ShapeOf
101
101
 
102
102
  def refute_shape_of(object, shape)
103
103
  if shape.respond_to? :shape_of?
104
- refute shape.shape_of? object
104
+ refute_operator shape, :shape_of?, object
105
105
  elsif shape.instance_of? ::Array
106
- refute Array[shape.first].shape_of? object
106
+ refute_operator Array[shape.first], :shape_of?, object
107
107
  elsif shape.instance_of? ::Hash
108
- refute Hash[shape].shape_of? object
108
+ refute_operator Hash[shape], :shape_of?, object
109
109
  else
110
110
  raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
111
111
  end
@@ -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 Array, are the core components of this module.
132
+ # This, along with Hash, 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].shape_of? elem
164
+ Array[@shape.first].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,7 +175,8 @@ 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. 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.
179
+ # Hash (without square brackets) is just a hash with any shape.
179
180
  # This, along with Array, are the core components of this module.
180
181
  # Note that the keys are converted to strings for comparison for both the shape and object provided.
181
182
  class Hash < Shape
@@ -222,7 +223,7 @@ module ShapeOf
222
223
  if @shape[key].respond_to? :shape_of?
223
224
  @shape[key].shape_of? elem
224
225
  elsif @shape[key].is_a? ::Array
225
- Array[@shape[key]].shape_of? elem
226
+ Array[@shape[key].first].shape_of? elem
226
227
  elsif @shape[key].is_a? ::Hash
227
228
  Hash[@shape[key]].shape_of? elem
228
229
  elsif @shape[key].is_a? Class
@@ -242,7 +243,7 @@ module ShapeOf
242
243
  end
243
244
  end
244
245
 
245
- # Union[Shape1, Shape2, ...] denotes that it can be of one the provided shapes
246
+ # Union[shape1, shape2, ...] denotes that it can be of one the provided shapes.
246
247
  class Union < Shape
247
248
  def self.shape_of?(object)
248
249
  false
@@ -284,7 +285,8 @@ module ShapeOf
284
285
  end
285
286
  end
286
287
 
287
- # 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)
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)
288
290
  class Optional < Shape
289
291
  def self.[](shape)
290
292
  raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass
@@ -303,13 +305,14 @@ module ShapeOf
303
305
  end
304
306
  end
305
307
 
308
+ # Anything matches unless key does not exist in the Hash.
306
309
  class Any < Shape
307
310
  def self.shape_of?(object)
308
311
  true
309
312
  end
310
313
  end
311
314
 
312
- # Nothing only passes when the key does not exist in the Hash.
315
+ # Only passes when the key does not exist in the Hash.
313
316
  class Nothing < Shape
314
317
  def self.shape_of?(object)
315
318
  false
@@ -320,10 +323,12 @@ module ShapeOf
320
323
  end
321
324
  end
322
325
 
326
+ # Union[Integer, Float, Rational, Complex]
323
327
  Numeric = Union[Integer, Float, Rational, Complex].tap do |this|
324
328
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Numeric'))
325
329
  end
326
330
 
331
+ # Union[TrueClass, FalseClass]
327
332
  Boolean = Union[TrueClass, FalseClass].tap do |this|
328
333
  this.instance_variable_set(:@class_name, this.name.sub(/Union.*/, 'Boolean'))
329
334
  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.0.2
4
+ version: 0.1.2
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-17 00:00:00.000000000 Z
11
+ date: 2021-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest