hash_police 0.0.1 → 0.0.2
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 +37 -41
- data/lib/hash_police/police.rb +7 -7
- data/lib/hash_police/version.rb +1 -1
- data/spec/hash_police/police_spec.rb +22 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cb08c7624e26e2f6dd120205fc763775529cb97
|
4
|
+
data.tar.gz: 1ab033f86e8b6a75057b604ee8adfc4be7642bef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cc7106d520b66cbc5652bbe1951b8df2dd9d516835f0f7abedf4a2f1a91020a4d84406b49c788873fb597d333a668716a4b1d8fc142e17281eb1cd072d4b61b
|
7
|
+
data.tar.gz: c5e41ea33a01c90740cb0115435511ce549e68aa34a83c31b90eb41ec995cb10b88099cd98f552052b1af368ff955a7930ffa48c3957cb23e3aa91538df05862
|
data/README.md
CHANGED
@@ -17,47 +17,43 @@ 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
|
-
result
|
53
|
-
|
54
|
-
result
|
55
|
-
|
56
|
-
result
|
57
|
-
result.passed? # => false
|
58
|
-
result.error_messages # => "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"
|
59
|
-
|
60
|
-
``
|
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"
|
61
57
|
|
62
58
|
## Contributing
|
63
59
|
|
data/lib/hash_police/police.rb
CHANGED
@@ -12,11 +12,7 @@ module HashPolice
|
|
12
12
|
def check target
|
13
13
|
result = CheckResult.new(context_key)
|
14
14
|
unless type_matched?(rule, target)
|
15
|
-
|
16
|
-
result.missing
|
17
|
-
else
|
18
|
-
result.differ_type(:expect => rule.class, :got => target.class)
|
19
|
-
end
|
15
|
+
result.differ_type(:expect => rule.class, :got => target.class)
|
20
16
|
return result
|
21
17
|
end
|
22
18
|
|
@@ -34,8 +30,12 @@ module HashPolice
|
|
34
30
|
target = stringify_keys(target)
|
35
31
|
|
36
32
|
rule.each do |rule_key, rule_val|
|
37
|
-
|
38
|
-
|
33
|
+
if target.has_key?(rule_key)
|
34
|
+
police = self.class.new(rule_val, "#{context_prefix}#{rule_key}")
|
35
|
+
result.concat(police.check(target[rule_key]))
|
36
|
+
else
|
37
|
+
result.missing("#{context_prefix}#{rule_key}")
|
38
|
+
end
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
data/lib/hash_police/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe HashPolice::Police do
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
describe "#check" do
|
11
|
+
describe "#check(target)" do
|
12
12
|
let(:police) { HashPolice::Police.new(rule) }
|
13
13
|
let(:rule) { "a string" }
|
14
14
|
let(:target) { rule.clone }
|
@@ -59,7 +59,7 @@ describe HashPolice::Police do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
context "when rule is a hash
|
62
|
+
context "when rule is a nested hash" do
|
63
63
|
let(:rule) do
|
64
64
|
{
|
65
65
|
:name => "Jack",
|
@@ -69,15 +69,17 @@ describe HashPolice::Police do
|
|
69
69
|
}
|
70
70
|
}
|
71
71
|
end
|
72
|
-
let(:
|
73
|
-
let(:
|
74
|
-
let(:
|
72
|
+
let(:result_name) { double(:result_name) }
|
73
|
+
let(:result_married) { double(:result_married) }
|
74
|
+
let(:result_nested) { double(:result_nested, :concat => nil) }
|
75
|
+
let(:result_key) { double(:result_key) }
|
75
76
|
|
76
77
|
before(:each) do
|
77
78
|
HashPolice::CheckResult.stub(:new).and_return(result,
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
result_name,
|
80
|
+
result_married,
|
81
|
+
result_nested,
|
82
|
+
result_key)
|
81
83
|
end
|
82
84
|
|
83
85
|
it "passes if all keys are matched" do
|
@@ -91,8 +93,19 @@ describe HashPolice::Police do
|
|
91
93
|
end
|
92
94
|
|
93
95
|
it "failed if missing key" do
|
96
|
+
HashPolice::CheckResult.stub(:new).and_return(result,
|
97
|
+
result_married,
|
98
|
+
result_nested,
|
99
|
+
result_key)
|
94
100
|
target.delete :name
|
95
|
-
|
101
|
+
result.should_receive(:missing).with("name")
|
102
|
+
|
103
|
+
police.check(target)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "failed if expect string but nil" do
|
107
|
+
target[:nested] = { :key => nil }
|
108
|
+
result_key.should_receive(:differ_type).with(:expect => String, :got => NilClass)
|
96
109
|
|
97
110
|
police.check(target)
|
98
111
|
end
|