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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c88a2f2754bcb69fb42484064e449b19ccfe298f
4
- data.tar.gz: 7090be452d009ad83b02332efb1728a5e5c4c0e7
3
+ metadata.gz: 2cb08c7624e26e2f6dd120205fc763775529cb97
4
+ data.tar.gz: 1ab033f86e8b6a75057b604ee8adfc4be7642bef
5
5
  SHA512:
6
- metadata.gz: 800ff7adfd4390e9e73ac23e8cdf3c98d6f149d381109eeec6002ecc2008ddfca33780be071ad0f5a58f34ab96f9f308b6816da09745745065c85b7da1073fcd
7
- data.tar.gz: 539f2ea73356ccf466e947de2e04b68b99f2f4d06196ebc7065ed1d157ac125fd562e063ff4fbe2c12d0cbee97d477cfad85001c44779db4bfa51fc1c5b4c138
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
- ``ruby
21
-
22
- rule = {
23
- :name => "a string",
24
- :age => 28,
25
- :favorites => [ "a string" ],
26
- :locations => [
27
- { :name => "string", :duration => 3 }
28
- ]
29
- }
30
-
31
- valid = {
32
- :name => "Jack",
33
- :age => 28,
34
- :favorites => [ "sport", "music" ],
35
- :locations => [
36
- { :name => "Taiwan", :duration => 25 },
37
- { :name => "US", :duration => 5 }
38
- ]
39
- }
40
-
41
- invalid = {
42
- :name => [],
43
- :age => "not a number",
44
- :locations => [
45
- { :name => "Taiwan", :duration => 25 },
46
- { :name => 23 }
47
- ]
48
- }
49
-
50
- police = HashPolice::Police.new(rule)
51
-
52
- result = police.check(valid)
53
- result.passed? # => true
54
- result.error_messages # => ""
55
-
56
- result = police.check(invalid)
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
 
@@ -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
- if context_key != "" && target.nil?
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
- police = self.class.new(rule_val, "#{context_prefix}#{rule_key}")
38
- result.concat(police.check(target[rule_key]))
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
@@ -1,3 +1,3 @@
1
1
  module HashPolice
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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 of scalar" do
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(:nested_result1) { double(:nested_result1) }
73
- let(:nested_result2) { double(:nested_result2) }
74
- let(:nested_result3) { double(:nested_result3, :concat => nil) }
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
- nested_result1,
79
- nested_result2,
80
- nested_result3)
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
- nested_result1.should_receive(:missing).with()
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_police
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yang-Hsing Lin