hash_assertions 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/hash_assertions.gemspec +27 -0
- data/lib/hash_assertions/assert_keys_with_values.rb +11 -0
- data/lib/hash_assertions/assert_required_keys.rb +9 -0
- data/lib/hash_assertions/version.rb +3 -0
- data/lib/hash_assertions.rb +3 -0
- data/spec/hash_assertions/assert_keys_with_values_spec.rb +54 -0
- data/spec/hash_assertions/assert_required_keys_spec.rb +44 -0
- data/spec/spec_helper.rb +5 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6e08b8f2329e6d39a20b165eda60a9866a8b973
|
4
|
+
data.tar.gz: 3a78f7206f45e7736d1fab0575fc9d537c4d02c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6b4c506c2dbe5b8db36c50308ed86c65cb371c2b7dffa4f66d566a95065a8b7b46ad77c211a7a026e24a518bd5383eed3a48d2fd70cb8307eeff803b8971d8a
|
7
|
+
data.tar.gz: 67f2718cbd7d40bcb7a655316c10850e6ee7cccf378740bfb525e363321e4edba352a9e021b33ab9f98aad6d5e9b07896ae87399eacb58d2c702c98d9e3a7739
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tomek Wałkuski
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# hash_assertions
|
2
|
+
|
3
|
+
Hash assertions: assert required keys, assert keys with values
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
gem "hash_assertions"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
``` sh
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
``` sh
|
22
|
+
$ gem install hash_assertions
|
23
|
+
```
|
24
|
+
|
25
|
+
and require it:
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
require "hash_assertions"
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### assert_required_keys
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
require "hash_assertions/assert_required_keys"
|
37
|
+
|
38
|
+
# Raises nothing
|
39
|
+
{ foo: "bar", baz: "qux" }.assert_required_keys(:foo, :baz)
|
40
|
+
|
41
|
+
# Raises ArgumentError, "Missing required key: baz"
|
42
|
+
{ foo: "bar" }.assert_required_keys(:foo, :baz)
|
43
|
+
|
44
|
+
# Raises ArgumentError, "Missing required key: baz"
|
45
|
+
{ foo: "bar", baz: "qux" }.assert_required_keys(:foo, "baz")
|
46
|
+
|
47
|
+
# Raises ArgumentError, "Missing required key: [:foo, :baz]"
|
48
|
+
# (assert_required_keys doesn't flatten arguments!)
|
49
|
+
{ foo: "bar", baz: "qux" }.assert_required_keys([:foo, :baz])
|
50
|
+
```
|
51
|
+
|
52
|
+
### assert_keys_with_values
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
require "hash_assertions/assert_keys_with_values"
|
56
|
+
|
57
|
+
# Raises nothing
|
58
|
+
{ foo: "bar", baz: "qux" }.assert_keys_with_values(:foo, :baz)
|
59
|
+
|
60
|
+
# Raises ArgumentError, "Blank value for key: baz"
|
61
|
+
{ foo: "bar", baz: nil }.assert_keys_with_values(:foo, :baz)
|
62
|
+
|
63
|
+
# Raises ArgumentError, "Blank value for key: baz"
|
64
|
+
{ foo: "bar", baz: "" }.assert_keys_with_values(:foo, :baz)
|
65
|
+
|
66
|
+
# Raises ArgumentError, "Blank value for key: baz"
|
67
|
+
{ foo: "bar", baz: "qux" }.assert_keys_with_values(:foo, "baz")
|
68
|
+
|
69
|
+
# Raises ArgumentError, "Blank value for key: [:foo, :baz]"
|
70
|
+
# (assert_keys_with_values doesn't flatten arguments!)
|
71
|
+
{ foo: "bar", baz: "qux" }.assert_keys_with_values([:foo, :baz])
|
72
|
+
```
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
|
7
|
+
require "hash_assertions/version"
|
8
|
+
|
9
|
+
Gem::Specification.new do |spec|
|
10
|
+
spec.name = "hash_assertions"
|
11
|
+
spec.version = HashAssertions::VERSION
|
12
|
+
spec.authors = ["Tomek Wałkuski"]
|
13
|
+
spec.email = ["ja@jestem.tw"]
|
14
|
+
spec.description = %q{Hash assertions}
|
15
|
+
spec.summary = %q{Hash assertions: assert required keys, assert keys with values}
|
16
|
+
spec.homepage = "https://github.com/tomekw/hash_assertions"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Hash
|
2
|
+
def assert_keys_with_values(*keys_with_values)
|
3
|
+
keys_with_values.each do |key_with_value|
|
4
|
+
required_value = fetch(key_with_value, nil)
|
5
|
+
|
6
|
+
if required_value.nil? || required_value.empty?
|
7
|
+
raise ArgumentError, "Blank value for key: #{key_with_value}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "hash_assertions/assert_keys_with_values"
|
3
|
+
|
4
|
+
describe "assert_keys_with_values" do
|
5
|
+
let(:hash) { { foo: "bar", baz: "qux", bar: nil, qux: "" } }
|
6
|
+
|
7
|
+
context "when all provided keys have present values" do
|
8
|
+
let(:present_keys?) { [:foo, :baz] }
|
9
|
+
|
10
|
+
it "passes" do
|
11
|
+
expect { hash.assert_keys_with_values(*present_keys?) }.to_not raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when some keys with nil values" do
|
16
|
+
let(:present_keys?) { [:baz, :bar] }
|
17
|
+
|
18
|
+
it "fails" do
|
19
|
+
expect do
|
20
|
+
hash.assert_keys_with_values(*present_keys?)
|
21
|
+
end.to raise_error(ArgumentError, "Blank value for key: bar")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when some keys with empty values" do
|
26
|
+
let(:present_keys?) { [:baz, :qux] }
|
27
|
+
|
28
|
+
it "fails" do
|
29
|
+
expect do
|
30
|
+
hash.assert_keys_with_values(*present_keys?)
|
31
|
+
end.to raise_error(ArgumentError, "Blank value for key: qux")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when using strings for keys" do
|
36
|
+
let(:present_keys?) { ["foo", "baz"] }
|
37
|
+
|
38
|
+
it "fails" do
|
39
|
+
expect do
|
40
|
+
hash.assert_keys_with_values(*present_keys?)
|
41
|
+
end.to raise_error(ArgumentError, "Blank value for key: foo")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when not splatting keys array" do
|
46
|
+
let(:present_keys?) { [:baz, :bar] }
|
47
|
+
|
48
|
+
it "fails" do
|
49
|
+
expect do
|
50
|
+
hash.assert_keys_with_values(present_keys?)
|
51
|
+
end.to raise_error(ArgumentError, "Blank value for key: [:baz, :bar]")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "hash_assertions/assert_required_keys"
|
3
|
+
|
4
|
+
describe "assert_required_keys" do
|
5
|
+
let(:hash) { { foo: "bar", baz: "qux" } }
|
6
|
+
|
7
|
+
context "when all required keys in hash" do
|
8
|
+
let(:required_keys) { [:foo, :baz] }
|
9
|
+
|
10
|
+
it "passes" do
|
11
|
+
expect { hash.assert_required_keys(*required_keys) }.to_not raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when some keys missing" do
|
16
|
+
let(:required_keys) { [:foo, :baz, :bar] }
|
17
|
+
|
18
|
+
it "fails" do
|
19
|
+
expect do
|
20
|
+
hash.assert_required_keys(*required_keys)
|
21
|
+
end.to raise_error(ArgumentError, "Missing required key: bar")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when using strings for required keys" do
|
26
|
+
let(:required_keys) { [:foo, "baz"] }
|
27
|
+
|
28
|
+
it "fails" do
|
29
|
+
expect do
|
30
|
+
hash.assert_required_keys(*required_keys)
|
31
|
+
end.to raise_error(ArgumentError, "Missing required key: baz")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when not splatting required keys array" do
|
36
|
+
let(:required_keys) { [:foo, :baz] }
|
37
|
+
|
38
|
+
it "fails" do
|
39
|
+
expect do
|
40
|
+
hash.assert_required_keys(required_keys)
|
41
|
+
end.to raise_error(ArgumentError, "Missing required key: [:foo, :baz]")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_assertions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomek Wałkuski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-08 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: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Hash assertions
|
56
|
+
email:
|
57
|
+
- ja@jestem.tw
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- hash_assertions.gemspec
|
69
|
+
- lib/hash_assertions.rb
|
70
|
+
- lib/hash_assertions/assert_keys_with_values.rb
|
71
|
+
- lib/hash_assertions/assert_required_keys.rb
|
72
|
+
- lib/hash_assertions/version.rb
|
73
|
+
- spec/hash_assertions/assert_keys_with_values_spec.rb
|
74
|
+
- spec/hash_assertions/assert_required_keys_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: https://github.com/tomekw/hash_assertions
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: 'Hash assertions: assert required keys, assert keys with values'
|
100
|
+
test_files:
|
101
|
+
- spec/hash_assertions/assert_keys_with_values_spec.rb
|
102
|
+
- spec/hash_assertions/assert_required_keys_spec.rb
|
103
|
+
- spec/spec_helper.rb
|