hashape 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/hashape.gemspec +28 -0
- data/lib/hashape.rb +108 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b37fe671e0f57877e73dd909a3cb44d102c2756dfffe18256dca966c606f7747
|
4
|
+
data.tar.gz: bd68bd8c108a93c2f31547cb54bda91e9828c88a94c04ed9abf650e5ee90806e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: be6614d94f50efeaa8616f002728da224f64d6c2ec8a77f906fd8a5a1bd3a9ceb9b0330c129a9291095fb8ef63cae55a08147b94750235552252e6b9f04f82de
|
7
|
+
data.tar.gz: c0f58ee27cfce42b168a93c68a614059dd0971f952cabb66117eb3d14e5ddcc341056d2ccf1a3c472b9b268d5af71a5fed090405db1650b0be008e9a24020e9c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hashape (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
docile (1.3.2)
|
11
|
+
json (2.2.0)
|
12
|
+
rake (10.5.0)
|
13
|
+
rspec (3.8.0)
|
14
|
+
rspec-core (~> 3.8.0)
|
15
|
+
rspec-expectations (~> 3.8.0)
|
16
|
+
rspec-mocks (~> 3.8.0)
|
17
|
+
rspec-core (3.8.1)
|
18
|
+
rspec-support (~> 3.8.0)
|
19
|
+
rspec-expectations (3.8.4)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.8.0)
|
22
|
+
rspec-mocks (3.8.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.8.0)
|
25
|
+
rspec-support (3.8.2)
|
26
|
+
simplecov (0.16.1)
|
27
|
+
docile (~> 1.1)
|
28
|
+
json (>= 1.8, < 3)
|
29
|
+
simplecov-html (~> 0.10.0)
|
30
|
+
simplecov-html (0.10.2)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
bundler (~> 2.0)
|
37
|
+
hashape!
|
38
|
+
rake (~> 10.0)
|
39
|
+
rspec (~> 3.0)
|
40
|
+
simplecov (~> 0.16.1)
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Aaron Christiansen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Hashape
|
2
|
+
|
3
|
+
Hashape is a **lightweight library for testing the structure of hashes**. It
|
4
|
+
supports deep nesting and a variety of different methods of matching types or
|
5
|
+
values (called _specifiers_).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hashape'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hashape
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## License
|
28
|
+
|
29
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/hashape.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hashape"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hashape'
|
8
|
+
spec.version = Hashape::VERSION
|
9
|
+
spec.authors = ['Aaron Christiansen']
|
10
|
+
spec.email = ['aaronc20000@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Easy validation of deeply-nested hashes'
|
13
|
+
spec.homepage = 'https://github.com/AaronC81/hashape'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/lib/hashape.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
##
|
2
|
+
# Provides utilities for checking the shape of deeply-nested hashes, such as
|
3
|
+
# for validating JSON.
|
4
|
+
module Hashape
|
5
|
+
##
|
6
|
+
# The library version.
|
7
|
+
VERSION = '1.0.0'
|
8
|
+
|
9
|
+
##
|
10
|
+
# Specifiers which can provide additional typing to shapes.
|
11
|
+
module Specifiers
|
12
|
+
##
|
13
|
+
# The basic specifier. You should probably never use this.
|
14
|
+
class Specifier
|
15
|
+
attr_reader :spec
|
16
|
+
|
17
|
+
##
|
18
|
+
# A shorthand for constructing an instance of any specifier.
|
19
|
+
# @param [*] spec The type spec for this specifier to match against.
|
20
|
+
# @return [Specifier] A new specifier.
|
21
|
+
def self.[](spec)
|
22
|
+
Specifier.new(spec)
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Creates a new specifier.
|
27
|
+
# @param [*] spec The type spec for this specifier to match against.
|
28
|
+
# @return [Specifier] A new specifier.
|
29
|
+
def initialize(spec)
|
30
|
+
@spec = spec
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# Allows a type spec to also be nil.
|
36
|
+
class Optional < Specifier
|
37
|
+
def ===(v)
|
38
|
+
v.nil? || self.spec === v
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Given a list of type specs, allows the value to match any one of those
|
44
|
+
# type specs.
|
45
|
+
class OneOf < Specifier
|
46
|
+
def ===(v)
|
47
|
+
raise 'spec for OneOf must be an array' unless self.spec.is_a?(Array)
|
48
|
+
self.spec.any? { |s| s === v }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Requires that the value be an enumerable, where each item matches the
|
54
|
+
# given type spec.
|
55
|
+
class Many < Specifier
|
56
|
+
def ===(v)
|
57
|
+
v.is_a?(Enumerable) && v.all? { |i| self.spec === i }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# A template hash representing how a hash should be structured, allowing
|
64
|
+
# other hashes to be validated against it.
|
65
|
+
class Shape
|
66
|
+
attr_reader :shape
|
67
|
+
|
68
|
+
##
|
69
|
+
# Create a new shape.
|
70
|
+
# @param [Hash] shape The template hash.
|
71
|
+
# @return [Shape] The new shape.
|
72
|
+
def initialize(shape)
|
73
|
+
@shape = shape
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Returns a boolean indicating whether the given hash matches the template
|
78
|
+
# hash which this shape was constructed with.
|
79
|
+
# @param [Hash] subject The hash to compare the template hash against.
|
80
|
+
# @return [TrueClass|FalseClass] A boolean indicating whether the subject
|
81
|
+
# hash matches the template hash.
|
82
|
+
def matches?(subject)
|
83
|
+
subject.all? do |k, v|
|
84
|
+
if v.is_a?(Hash) && shape[k].is_a?(Hash)
|
85
|
+
Shape.new(shape[k]).matches?(v)
|
86
|
+
else
|
87
|
+
shape[k] === v
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# Calls #matches? and raises a RuntimeError if it does not return true.
|
94
|
+
# @param [Hash] subject The hash to compare the template hash against.
|
95
|
+
def matches!(subject)
|
96
|
+
raise unless matches?(subject)
|
97
|
+
nil
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
##
|
102
|
+
# Create a new shape.
|
103
|
+
# @param [Hash] shape The template hash.
|
104
|
+
# @return [Shape] The new shape.
|
105
|
+
def self.shape(shape)
|
106
|
+
Shape.new(shape)
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashape
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Christiansen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- aaronc20000@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- hashape.gemspec
|
70
|
+
- lib/hashape.rb
|
71
|
+
homepage: https://github.com/AaronC81/hashape
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubygems_version: 3.0.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Easy validation of deeply-nested hashes
|
94
|
+
test_files: []
|