hash_police 0.0.3 → 0.0.4
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/README.md +54 -37
- data/lib/hash_police/police.rb +1 -0
- data/lib/hash_police/rspec_matcher.rb +13 -0
- data/lib/hash_police/version.rb +1 -1
- data/spec/hash_police/check_result_spec.rb +1 -1
- data/spec/hash_police/matcher_spec.rb +11 -0
- data/spec/hash_police/police_spec.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f65798cbb95f63baf5b4f55050286e2851afee7e
|
4
|
+
data.tar.gz: 3593e76d36b0ad927a599326674e9e586ab15baa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d12c3d9b8750e6c56a5eb82614fe57a65c062da82a1c3228bc4c11b2267b770b673b39fc412dca63a90cba9789f8c8acfec9fdd3d7fdded8ebc4d4e7b4861cd8
|
7
|
+
data.tar.gz: 0493195b05a025b247ae87b09b437af736a7236b0da1e161845f9440e1b962bd7e2ed453e9a5154cb25ce5c3fd4ebfc72caf1609e6ef541a41ef45da3e623d6a
|
data/README.md
CHANGED
@@ -17,43 +17,60 @@ Or install it yourself as:
|
|
17
17
|
|
18
18
|
## Usage
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
20
|
+
```ruby
|
21
|
+
rule = {
|
22
|
+
:name => "a string",
|
23
|
+
:age => 28,
|
24
|
+
:favorites => [ "a string" ],
|
25
|
+
:locations => [
|
26
|
+
{ :name => "string", :duration => 3 }
|
27
|
+
]
|
28
|
+
}
|
29
|
+
|
30
|
+
valid = {
|
31
|
+
:name => "Jack",
|
32
|
+
:age => 28,
|
33
|
+
:favorites => [ "sport", "music" ],
|
34
|
+
:locations => [
|
35
|
+
{ :name => "Taiwan", :duration => 25 },
|
36
|
+
{ :name => "US", :duration => 5 }
|
37
|
+
]
|
38
|
+
}
|
39
|
+
|
40
|
+
invalid = {
|
41
|
+
:name => [],
|
42
|
+
:age => "not a number",
|
43
|
+
:locations => [
|
44
|
+
{ :name => "Taiwan", :duration => 25 },
|
45
|
+
{ :name => 23 }
|
46
|
+
]
|
47
|
+
}
|
48
|
+
|
49
|
+
police = HashPolice::Police.new(rule)
|
50
|
+
|
51
|
+
result = police.check(valid)
|
52
|
+
result.passed? # => true
|
53
|
+
result.error_messages # => ""
|
54
|
+
|
55
|
+
result = police.check(invalid)
|
56
|
+
result.passed? # => false
|
57
|
+
result.error_messages #=> "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"
|
58
|
+
```
|
59
|
+
|
60
|
+
## RSpec matcher:
|
61
|
+
|
62
|
+
`HashPolice` provides a RSpec matcher `have_the_same_hash_format_as` checking the formats of two hashes.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'hash_police/rspec_matcher'
|
66
|
+
|
67
|
+
it "should be able to use the matcher `have_the_same_hash_format_as`" do
|
68
|
+
expected = { 'str' => '', 'an_arr' => [ 1 ] }
|
69
|
+
to_be_checked = { 'str' => 'hola', :an_arr => [1,3,4] }
|
70
|
+
|
71
|
+
expect(to_be_checked).to have_the_same_hash_format_as(expected)
|
72
|
+
end
|
73
|
+
```
|
57
74
|
|
58
75
|
## Contributing
|
59
76
|
|
data/lib/hash_police/police.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
RSpec::Matchers.define :have_the_same_hash_format_as do |expected|
|
2
|
+
result = nil
|
3
|
+
|
4
|
+
match do |actual|
|
5
|
+
police = HashPolice::Police.new(expected)
|
6
|
+
result = police.check(actual)
|
7
|
+
result.passed?
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message do |actual|
|
11
|
+
result.error_messages
|
12
|
+
end
|
13
|
+
end
|
data/lib/hash_police/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'hash_police'
|
2
|
+
require 'hash_police/rspec_matcher'
|
3
|
+
|
4
|
+
describe 'Matcher' do
|
5
|
+
it "should be able to use the matcher `have_the_same_hash_format_as`" do
|
6
|
+
expected = { 'str' => '', 'an_arr' => [ 1 ] }
|
7
|
+
to_be_checked = { 'str' => 'hola', :an_arr => [1,3,4] }
|
8
|
+
|
9
|
+
expect(to_be_checked).to have_the_same_hash_format_as(expected)
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_police
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yang-Hsing Lin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,8 +69,10 @@ files:
|
|
69
69
|
- lib/hash_police.rb
|
70
70
|
- lib/hash_police/check_result.rb
|
71
71
|
- lib/hash_police/police.rb
|
72
|
+
- lib/hash_police/rspec_matcher.rb
|
72
73
|
- lib/hash_police/version.rb
|
73
74
|
- spec/hash_police/check_result_spec.rb
|
75
|
+
- spec/hash_police/matcher_spec.rb
|
74
76
|
- spec/hash_police/police_spec.rb
|
75
77
|
homepage: ''
|
76
78
|
licenses:
|
@@ -92,10 +94,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
requirements: []
|
94
96
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.2
|
96
98
|
signing_key:
|
97
99
|
specification_version: 4
|
98
100
|
summary: a gem to check whether given to hashes are of the same format
|
99
101
|
test_files:
|
100
102
|
- spec/hash_police/check_result_spec.rb
|
103
|
+
- spec/hash_police/matcher_spec.rb
|
101
104
|
- spec/hash_police/police_spec.rb
|