shape_of 0.0.1 → 1.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 +48 -11
  3. metadata +33 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c42b5e14e8ba448eac77c5fc6f3ad8c71dd20577fcdfd9bb12611d5d9c61aa2
4
- data.tar.gz: c8d658a759422c4780102bdf15845f90c9abb2ec02397e37872d8733b7243500
3
+ metadata.gz: 6e28929cd043b2227c3fcfc7ec697afeebdcd77aa4fe82f3239c0c70518a653b
4
+ data.tar.gz: ea1433d60b53e1cf9793e5824a02d08d990d079ab67d62684dbf5a9a8278227c
5
5
  SHA512:
6
- metadata.gz: f788452f8b9d53c5c76fafb9bb9af3da10bf2fce527dd95a6d6ac26b9c0b6bad27d4df584e832e2f76f839d7518f34e599b0b9710358f90b55bd95f022ae816f
7
- data.tar.gz: 63da2ef6a2b5d7490701e7e7f610319a594f73bb3a9fc918bfed1b71f0ca6bdf44770a74ab6afe1de740708e096a28367639e54c6a3ed9cdee59a9fbfe39adc8
6
+ metadata.gz: 1f4a6d76316870ae8945faa6de5797647c9aa9cbe259017b7acaf773a75c6100dfea4770d1f290dd2e8abcd37293111b74a6beb60767db6814a4ffab7714fec5
7
+ data.tar.gz: 8dae349aab3980850618761687ad7b3e61701dd9e29ebdc6880757bb744dd5f708e5a63d931de5121777e619054c7cdddfd8931416cd40a5960448bd89c3e60e
data/lib/shape_of.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Copyright 2021 John Isom.
2
- # Licensed under the MIT open source license.
2
+ # Licensed under the open source MIT license.
3
3
 
4
4
  # The ShapeOf module can be used in testing JSON APIs to make sure that
5
5
  # the body of the result is of the correct format. It is similar to a type
@@ -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
@@ -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,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shape_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.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-14 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-reporters
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
13
41
  description:
14
42
  email: john@johnisom.dev
15
43
  executables: []
@@ -17,7 +45,7 @@ extensions: []
17
45
  extra_rdoc_files: []
18
46
  files:
19
47
  - lib/shape_of.rb
20
- homepage:
48
+ homepage: https://github.com/johnisom/shape_of
21
49
  licenses:
22
50
  - MIT
23
51
  metadata: {}
@@ -36,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
64
  - !ruby/object:Gem::Version
37
65
  version: '0'
38
66
  requirements: []
39
- rubygems_version: 3.2.15
67
+ rubygems_version: 3.2.17
40
68
  signing_key:
41
69
  specification_version: 4
42
70
  summary: A shape/type checker for Ruby objects.