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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad058f96fd412a77400f8cf0a3d034394bd7cd88
4
- data.tar.gz: 5d09df0ade02fc80b6b52706e59a26c22a013b0f
3
+ metadata.gz: f65798cbb95f63baf5b4f55050286e2851afee7e
4
+ data.tar.gz: 3593e76d36b0ad927a599326674e9e586ab15baa
5
5
  SHA512:
6
- metadata.gz: 030915243afc73a524f67166afbb1d5f440cd8cd1e539a1b37d30f541fb9bb038f4ed4ec0954beb00e18be6133a61217a2143c38a3e126ec5c40251f8b8209ae
7
- data.tar.gz: 3687760e41798d3e768503a1432eacdc7887bd2654971bc737cf18991e39c1848b9040175d0f628fb8797c69566f2c2bce7def80162ae219fb95a23951605ba7
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
- rule = {
21
- :name => "a string",
22
- :age => 28,
23
- :favorites => [ "a string" ],
24
- :locations => [
25
- { :name => "string", :duration => 3 }
26
- ]
27
- }
28
-
29
- valid = {
30
- :name => "Jack",
31
- :age => 28,
32
- :favorites => [ "sport", "music" ],
33
- :locations => [
34
- { :name => "Taiwan", :duration => 25 },
35
- { :name => "US", :duration => 5 }
36
- ]
37
- }
38
-
39
- invalid = {
40
- :name => [],
41
- :age => "not a number",
42
- :locations => [
43
- { :name => "Taiwan", :duration => 25 },
44
- { :name => 23 }
45
- ]
46
- }
47
-
48
- police = HashPolice::Police.new(rule)
49
-
50
- result = police.check(valid)
51
- result.passed? # => true
52
- result.error_messages # => ""
53
-
54
- result = police.check(invalid)
55
- result.passed? # => false
56
- result.error_messages # => "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"
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
 
@@ -1,4 +1,5 @@
1
1
  require "hash_police/check_result"
2
+ require 'json'
2
3
 
3
4
  module HashPolice
4
5
  class Police
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HashPolice
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,4 +1,4 @@
1
- require "hash_police/check_result"
1
+ require "hash_police"
2
2
 
3
3
  describe HashPolice::CheckResult do
4
4
  let(:context_key) { "the-key" }
@@ -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
@@ -1,4 +1,4 @@
1
- require "hash_police/police"
1
+ require "hash_police"
2
2
  require "json"
3
3
 
4
4
  describe HashPolice::Police do
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.3
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-01-14 00:00:00.000000000 Z
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.0.3
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