shaped 0.6.4 → 0.7.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +12 -1
- data/Gemfile.lock +6 -6
- data/README.md +39 -5
- data/lib/shaped.rb +2 -1
- data/lib/shaped/shapes/all.rb +25 -0
- data/lib/shaped/shapes/{or.rb → any.rb} +1 -1
- data/lib/shaped/shapes/method.rb +15 -0
- data/lib/shaped/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6243debb70327afd9840f95f340bafd00f2de355e247bba0c6f16f3328c11e6b
|
4
|
+
data.tar.gz: b993c1b5252874d7d76c845bac27331eacf6de59f24b5f6aa8da2e8caa5ff1c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0feed58b9c6ac20113a75ec9d560b69102a8240e192b6ce353d6a2da2e82ca01b9aa6d80422ad0565e3a367537c2bb8de9fe10c7242969a8f56a0ff8cabe824a
|
7
|
+
data.tar.gz: 2dff65af38ece59c0ad34309f8111606cbc7bee03a5be912561965f0e8557eef5253d50d872317a8d0a97669c8a0c98b4906ca57696d194c8d293fa0e37eb005
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
## v0.7.0 (2020-06-24)
|
2
|
+
### BREAKING CHANGES
|
3
|
+
- Rename the `Or` shape to `Any`
|
4
|
+
- Add a `Method` shape (where the shape description is the name of a method which, when called on a
|
5
|
+
test object, must return a truthy value). This is a breaking change because the `Shaped::Shape`
|
6
|
+
constructor will now return an instance of `Shaped::Shapes::Method` rather than
|
7
|
+
`Shaped::Shapes::Equality` when called with a Symbol argument.
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Add an `All` shape (w/ multiple sub-shapes, all of which must be matched)
|
11
|
+
|
1
12
|
## v0.6.4 (2020-06-22)
|
2
13
|
### Fixed
|
3
14
|
- Make it possible to specify optional keys in a Hash shape (using an `Or` shape as the value)
|
@@ -92,7 +103,7 @@
|
|
92
103
|
`Shaped::Shapes::Hash`):
|
93
104
|
1. `Shaped::Shapes::Class`
|
94
105
|
2. `Shaped::Shapes::Equality`
|
95
|
-
3. `Shaped::Shapes::
|
106
|
+
3. `Shaped::Shapes::Any`
|
96
107
|
- All hashes and arrays in shape definitions are parsed "recursively" as shape definitions. For
|
97
108
|
example, instead of:
|
98
109
|
|
data/Gemfile.lock
CHANGED
@@ -10,7 +10,7 @@ GIT
|
|
10
10
|
PATH
|
11
11
|
remote: .
|
12
12
|
specs:
|
13
|
-
shaped (0.
|
13
|
+
shaped (0.7.0)
|
14
14
|
activemodel (~> 6.0)
|
15
15
|
activesupport (~> 6.0)
|
16
16
|
|
@@ -61,9 +61,9 @@ GEM
|
|
61
61
|
notiffany (0.1.3)
|
62
62
|
nenv (~> 0.1)
|
63
63
|
shellany (~> 0.0)
|
64
|
-
parallel (1.19.
|
65
|
-
parser (2.7.1.
|
66
|
-
ast (~> 2.4.
|
64
|
+
parallel (1.19.2)
|
65
|
+
parser (2.7.1.4)
|
66
|
+
ast (~> 2.4.1)
|
67
67
|
pry (0.13.1)
|
68
68
|
coderay (~> 1.1)
|
69
69
|
method_source (~> 1.0)
|
@@ -90,13 +90,13 @@ GEM
|
|
90
90
|
diff-lcs (>= 1.2.0, < 2.0)
|
91
91
|
rspec-support (~> 3.9.0)
|
92
92
|
rspec-support (3.9.3)
|
93
|
-
rubocop (0.
|
93
|
+
rubocop (0.86.0)
|
94
94
|
parallel (~> 1.10)
|
95
95
|
parser (>= 2.7.0.1)
|
96
96
|
rainbow (>= 2.2.2, < 4.0)
|
97
97
|
regexp_parser (>= 1.7)
|
98
98
|
rexml
|
99
|
-
rubocop-ast (>= 0.0.3)
|
99
|
+
rubocop-ast (>= 0.0.3, < 1.0)
|
100
100
|
ruby-progressbar (~> 1.7)
|
101
101
|
unicode-display_width (>= 1.4.0, < 2.0)
|
102
102
|
rubocop-ast (0.0.3)
|
data/README.md
CHANGED
@@ -21,15 +21,17 @@ Validate the "shape" of Ruby objects!
|
|
21
21
|
* [Shaped::Shapes::Array](#shapedshapesarray)
|
22
22
|
* [Shaped::Shapes::Class](#shapedshapesclass)
|
23
23
|
* [ActiveModel validations](#activemodel-validations)
|
24
|
+
* [Shaped::Shapes::Method](#shapedshapesmethod)
|
24
25
|
* [Shaped::Shapes::Callable](#shapedshapescallable)
|
25
26
|
* [Shaped::Shapes::Equality](#shapedshapesequality)
|
26
|
-
* [Shaped::Shapes::
|
27
|
+
* [Shaped::Shapes::Any](#shapedshapesany)
|
28
|
+
* [Shaped::Shapes::All](#shapedshapesall)
|
27
29
|
* [#to_s](#to_s)
|
28
30
|
* [Development](#development)
|
29
31
|
* [For maintainers](#for-maintainers)
|
30
32
|
* [License](#license)
|
31
33
|
|
32
|
-
<!-- Added by: david, at:
|
34
|
+
<!-- Added by: david, at: Wed Jun 24 13:56:49 PDT 2020 -->
|
33
35
|
|
34
36
|
<!--te-->
|
35
37
|
|
@@ -91,7 +93,7 @@ types (all of which inherit from `Shaped::Shape`):
|
|
91
93
|
1. `Shaped::Shapes::Class`
|
92
94
|
1. `Shaped::Shapes::Callable`
|
93
95
|
1. `Shaped::Shapes::Equality`
|
94
|
-
1. `Shaped::Shapes::
|
96
|
+
1. `Shaped::Shapes::Any`
|
95
97
|
|
96
98
|
Examples illustrating the use of each shape type are below.
|
97
99
|
|
@@ -204,6 +206,21 @@ shape.matched_by?('a@b.c') # too short
|
|
204
206
|
# => false
|
205
207
|
```
|
206
208
|
|
209
|
+
## Shaped::Shapes::Method
|
210
|
+
|
211
|
+
This shape allows specifying a method name that, when called upon a test object, must return a
|
212
|
+
truthy value in order for `matched_by?` to be true.
|
213
|
+
|
214
|
+
```rb
|
215
|
+
shape = Shaped::Shape(:odd?)
|
216
|
+
|
217
|
+
shape.matched_by?(55)
|
218
|
+
# => true
|
219
|
+
|
220
|
+
shape.matched_by?(60)
|
221
|
+
# => false
|
222
|
+
```
|
223
|
+
|
207
224
|
## Shaped::Shapes::Callable
|
208
225
|
|
209
226
|
This shape is very powerful if you need a very customized shape definition; you can define any
|
@@ -282,7 +299,7 @@ shape.matched_by?(verification_code: '321cba', new_role: 'SuperAdmin')
|
|
282
299
|
# => false
|
283
300
|
```
|
284
301
|
|
285
|
-
## Shaped::Shapes::
|
302
|
+
## Shaped::Shapes::Any
|
286
303
|
|
287
304
|
This shape is used behind the scenes to build "compound matchers", such as an Array shape that
|
288
305
|
allows multiple different classes:
|
@@ -299,7 +316,7 @@ shape.matched_by?([0.333, 55])
|
|
299
316
|
# => false
|
300
317
|
```
|
301
318
|
|
302
|
-
You can build an `
|
319
|
+
You can build an `Any` shape by invoking the `Shaped::Shape` constructor with more than one argument.
|
303
320
|
Below is a (rather artificial) example illustrating this. To match this `shape`, an object must be
|
304
321
|
either greater than zero OR an Integer (or both).
|
305
322
|
|
@@ -316,6 +333,23 @@ shape.matched_by?(-11.5) # it's neither greater than 0 nor an Integer
|
|
316
333
|
# => false
|
317
334
|
```
|
318
335
|
|
336
|
+
## Shaped::Shapes::All
|
337
|
+
|
338
|
+
This shape can be used to combine multiple checks, all of which must be true for a test object in
|
339
|
+
order for `#matched_by?` to be true:
|
340
|
+
|
341
|
+
```rb
|
342
|
+
shape = Shaped::Shapes::All.new(Numeric, ->(number) { number.even? })
|
343
|
+
shape.to_s
|
344
|
+
# => "Numeric AND Proc test defined at (eval):10"
|
345
|
+
|
346
|
+
shape.matched_by?(22) # 22 is both a Numeric and `#even?`
|
347
|
+
# => true
|
348
|
+
|
349
|
+
shape.matched_by?(33) # 33 is a Numeric but it's not `#even?`
|
350
|
+
# => false
|
351
|
+
```
|
352
|
+
|
319
353
|
## `#to_s`
|
320
354
|
|
321
355
|
Each Shape type implements a `#to_s` instance method that aims to provide a relatively clear
|
data/lib/shaped.rb
CHANGED
@@ -13,7 +13,7 @@ module Shaped
|
|
13
13
|
def self.Shape(*shape_descriptions)
|
14
14
|
validation_options = shape_descriptions.extract_options!
|
15
15
|
if shape_descriptions.size >= 2
|
16
|
-
Shaped::Shapes::
|
16
|
+
Shaped::Shapes::Any.new(*shape_descriptions, validation_options)
|
17
17
|
else
|
18
18
|
# If the shape_descriptions argument list was just one hash, then `extract_options!` would
|
19
19
|
# have removed it, making `shape_descriptions` an empty array, so we need to "restore" the
|
@@ -30,6 +30,7 @@ module Shaped
|
|
30
30
|
when Shaped::Shape then shape_description
|
31
31
|
when Hash then Shaped::Shapes::Hash.new(shape_description)
|
32
32
|
when Array then Shaped::Shapes::Array.new(shape_description)
|
33
|
+
when Symbol then Shaped::Shapes::Method.new(shape_description)
|
33
34
|
when Class then Shaped::Shapes::Class.new(shape_description, validation_options)
|
34
35
|
else
|
35
36
|
if shape_description.respond_to?(:call)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Shaped::Shapes::All < Shaped::Shape
|
4
|
+
def initialize(*shape_descriptions)
|
5
|
+
validation_options = shape_descriptions.extract_options!
|
6
|
+
if shape_descriptions.size <= 1
|
7
|
+
raise(Shaped::InvalidShapeDescription, <<~ERROR.squish)
|
8
|
+
A #{self.class} description must be a list of two or more shape descriptions.
|
9
|
+
ERROR
|
10
|
+
end
|
11
|
+
|
12
|
+
@shapes =
|
13
|
+
shape_descriptions.map do |description|
|
14
|
+
Shaped::Shape(description, validation_options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def matched_by?(object)
|
19
|
+
@shapes.all? { |shape| shape.matched_by?(object) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@shapes.map(&:to_s).join(' AND ')
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Shaped::Shapes::Method < Shaped::Shape
|
4
|
+
def initialize(method_name)
|
5
|
+
@method_name = method_name
|
6
|
+
end
|
7
|
+
|
8
|
+
def matched_by?(object)
|
9
|
+
!!object.public_send(@method_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"object returning truthy for ##{@method_name}"
|
14
|
+
end
|
15
|
+
end
|
data/lib/shaped/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shaped
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Runger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -65,12 +65,14 @@ files:
|
|
65
65
|
- bin/setup
|
66
66
|
- lib/shaped.rb
|
67
67
|
- lib/shaped/shape.rb
|
68
|
+
- lib/shaped/shapes/all.rb
|
69
|
+
- lib/shaped/shapes/any.rb
|
68
70
|
- lib/shaped/shapes/array.rb
|
69
71
|
- lib/shaped/shapes/callable.rb
|
70
72
|
- lib/shaped/shapes/class.rb
|
71
73
|
- lib/shaped/shapes/equality.rb
|
72
74
|
- lib/shaped/shapes/hash.rb
|
73
|
-
- lib/shaped/shapes/
|
75
|
+
- lib/shaped/shapes/method.rb
|
74
76
|
- lib/shaped/version.rb
|
75
77
|
- shaped.gemspec
|
76
78
|
homepage: https://github.com/davidrunger/shaped
|