hash_police 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +1 -0
- data/hash_police.gemspec +24 -0
- data/lib/hash_police/check_result.rb +35 -0
- data/lib/hash_police/police.rb +65 -0
- data/lib/hash_police/version.rb +3 -0
- data/lib/hash_police.rb +6 -0
- data/spec/hash_police/check_result_spec.rb +71 -0
- data/spec/hash_police/police_spec.rb +101 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c88a2f2754bcb69fb42484064e449b19ccfe298f
|
4
|
+
data.tar.gz: 7090be452d009ad83b02332efb1728a5e5c4c0e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 800ff7adfd4390e9e73ac23e8cdf3c98d6f149d381109eeec6002ecc2008ddfca33780be071ad0f5a58f34ab96f9f308b6816da09745745065c85b7da1073fcd
|
7
|
+
data.tar.gz: 539f2ea73356ccf466e947de2e04b68b99f2f4d06196ebc7065ed1d157ac125fd562e063ff4fbe2c12d0cbee97d477cfad85001c44779db4bfa51fc1c5b4c138
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Yang-Hsing Lin
|
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,68 @@
|
|
1
|
+
# HashPolice
|
2
|
+
A gem to check whether given to hashes are of the same format
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'hash_police'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install hash_police
|
17
|
+
|
18
|
+
## Usage
|
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
|
+
``
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hash_police.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_police/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_police"
|
8
|
+
spec.version = HashPolice::VERSION
|
9
|
+
spec.authors = ["Yang-Hsing Lin"]
|
10
|
+
spec.email = ["yanghsing.lin@gmail.com"]
|
11
|
+
spec.description = %q{a gem to check whether given to hashes are of the same format}
|
12
|
+
spec.summary = %q{a gem to check whether given to hashes are of the same format}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class HashPolice::CheckResult
|
2
|
+
attr_reader :context_key
|
3
|
+
|
4
|
+
def initialize context_key
|
5
|
+
@context_key = context_key
|
6
|
+
@errors = []
|
7
|
+
@children = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def differ_type options
|
11
|
+
@errors << "`#{context_key}`: expect #{options[:expect]}, got #{options[:got]}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_messages
|
15
|
+
all_errors.join("; ")
|
16
|
+
end
|
17
|
+
|
18
|
+
def all_errors
|
19
|
+
@children.reduce(@errors) do |memo, child|
|
20
|
+
memo + child.all_errors
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def concat child_result
|
25
|
+
@children << child_result
|
26
|
+
end
|
27
|
+
|
28
|
+
def missing
|
29
|
+
@errors << "`#{context_key}`: missing"
|
30
|
+
end
|
31
|
+
|
32
|
+
def passed?
|
33
|
+
all_errors.empty?
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "hash_police/check_result"
|
2
|
+
|
3
|
+
module HashPolice
|
4
|
+
class Police
|
5
|
+
attr_reader :rule, :context_key
|
6
|
+
def initialize rule, context_key = ""
|
7
|
+
@rule = rule
|
8
|
+
@passed = false
|
9
|
+
@context_key = context_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def check target
|
13
|
+
result = CheckResult.new(context_key)
|
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
|
20
|
+
return result
|
21
|
+
end
|
22
|
+
|
23
|
+
unless scalar?(rule)
|
24
|
+
context_prefix = context_key == "" ? "" : "#{context_key}."
|
25
|
+
if rule.kind_of?(Array)
|
26
|
+
target.each_with_index do |t, index|
|
27
|
+
police = self.class.new(rule.first, "#{context_prefix}#{index}")
|
28
|
+
result.concat(police.check(t))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if rule.kind_of?(Hash)
|
33
|
+
rule = stringify_keys(self.rule)
|
34
|
+
target = stringify_keys(target)
|
35
|
+
|
36
|
+
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]))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def type_matched? rule, target
|
48
|
+
return true if bool?(rule) && bool?(target)
|
49
|
+
rule.class == target.class
|
50
|
+
end
|
51
|
+
|
52
|
+
def bool? val
|
53
|
+
!! val == val
|
54
|
+
end
|
55
|
+
|
56
|
+
def scalar? val
|
57
|
+
! val.kind_of?(Array) && ! val.kind_of?(Hash)
|
58
|
+
end
|
59
|
+
|
60
|
+
def stringify_keys hash
|
61
|
+
JSON.parse(hash.to_json, :quirks_mode => true)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/lib/hash_police.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "hash_police/check_result"
|
2
|
+
|
3
|
+
describe HashPolice::CheckResult do
|
4
|
+
let(:context_key) { "the-key" }
|
5
|
+
let(:result) { HashPolice::CheckResult.new context_key }
|
6
|
+
|
7
|
+
describe "::new(context_key)" do
|
8
|
+
it "takes a context_key to init" do
|
9
|
+
result = HashPolice::CheckResult.new "the-key"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#error_messages" do
|
14
|
+
context "when without children" do
|
15
|
+
it "returns plain reason" do
|
16
|
+
result.differ_type(:expect => String, :got => Fixnum)
|
17
|
+
result.error_messages.should == "`the-key`: expect String, got Fixnum"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when with children" do
|
22
|
+
it "returns message with children's ones" do
|
23
|
+
children = [1,2,3].map do |i|
|
24
|
+
child = HashPolice::CheckResult.new("key-#{i}")
|
25
|
+
result.concat(child)
|
26
|
+
child
|
27
|
+
end
|
28
|
+
|
29
|
+
children[1].differ_type(:expect => String, :got => Fixnum)
|
30
|
+
children[2].missing
|
31
|
+
|
32
|
+
result.error_messages.should == "`key-2`: expect String, got Fixnum; `key-3`: missing"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#missing" do
|
39
|
+
it "adds missing message to errors" do
|
40
|
+
result.missing
|
41
|
+
|
42
|
+
result.error_messages.should == "`#{context_key}`: missing"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#differ_type(:expect => Class1, :got => Class2)" do
|
47
|
+
it "adds message to error_messages" do
|
48
|
+
result.differ_type(:expect => String, :got => Array)
|
49
|
+
result.error_messages.should == "`#{context_key}`: expect String, got Array"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "passed?" do
|
54
|
+
it "returns true if all_errors empty" do
|
55
|
+
result.passed?.should == true
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns false if all_errors not empty" do
|
59
|
+
result.missing
|
60
|
+
|
61
|
+
result.passed?.should == false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#concat(result)" do
|
66
|
+
it "concat the child with its message" do
|
67
|
+
child = double
|
68
|
+
result.concat(child)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require "hash_police/police"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
describe HashPolice::Police do
|
5
|
+
describe "::new(rule)" do
|
6
|
+
it "takes a rule hash to init" do
|
7
|
+
police = HashPolice::Police.new({ :hello => "world" })
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#check" do
|
12
|
+
let(:police) { HashPolice::Police.new(rule) }
|
13
|
+
let(:rule) { "a string" }
|
14
|
+
let(:target) { rule.clone }
|
15
|
+
let(:result) { double(:result, :concat => nil ) }
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
HashPolice::CheckResult.stub(:new).and_return(result)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when rule is scalar value" do
|
22
|
+
it "passes if a string" do
|
23
|
+
HashPolice::CheckResult.stub(:new => double)
|
24
|
+
police.check "another string"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "failed if type not match" do
|
28
|
+
result.should_receive(:differ_type).with(:expect => String, :got => Fixnum)
|
29
|
+
police.check 12345
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when rule is an array of scalar" do
|
34
|
+
let(:rule) { [ 1 ] }
|
35
|
+
let(:nested_result1) { double(:nested_result1) }
|
36
|
+
let(:nested_result2) { double(:nested_result2) }
|
37
|
+
|
38
|
+
before(:each) do
|
39
|
+
HashPolice::CheckResult.stub(:new).and_return(result,
|
40
|
+
nested_result1,
|
41
|
+
nested_result2)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "passes if target is an of the same type" do
|
45
|
+
police.check [ 1, 3, 4 ]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "failed if target is not an array" do
|
49
|
+
result.should_receive(:differ_type).with(:expect => Array, :got => Fixnum)
|
50
|
+
police.check 12345
|
51
|
+
end
|
52
|
+
|
53
|
+
it "faild if not all elements are of the same type" do
|
54
|
+
result.should_receive(:concat).with(nested_result1)
|
55
|
+
result.should_receive(:concat).with(nested_result2)
|
56
|
+
nested_result2.should_receive(:differ_type).with(:expect => Fixnum, :got => String)
|
57
|
+
|
58
|
+
police.check [ 123, "string" ]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when rule is a hash of scalar" do
|
63
|
+
let(:rule) do
|
64
|
+
{
|
65
|
+
:name => "Jack",
|
66
|
+
:married => true,
|
67
|
+
:nested => {
|
68
|
+
:key => "val"
|
69
|
+
}
|
70
|
+
}
|
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) }
|
75
|
+
|
76
|
+
before(:each) do
|
77
|
+
HashPolice::CheckResult.stub(:new).and_return(result,
|
78
|
+
nested_result1,
|
79
|
+
nested_result2,
|
80
|
+
nested_result3)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "passes if all keys are matched" do
|
84
|
+
police.check(target)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "passes if target with stringed key" do
|
88
|
+
target["name"] = target.delete :name
|
89
|
+
|
90
|
+
police.check(target)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "failed if missing key" do
|
94
|
+
target.delete :name
|
95
|
+
nested_result1.should_receive(:missing).with()
|
96
|
+
|
97
|
+
police.check(target)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_police
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yang-Hsing Lin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 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: a gem to check whether given to hashes are of the same format
|
56
|
+
email:
|
57
|
+
- yanghsing.lin@gmail.com
|
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_police.gemspec
|
69
|
+
- lib/hash_police.rb
|
70
|
+
- lib/hash_police/check_result.rb
|
71
|
+
- lib/hash_police/police.rb
|
72
|
+
- lib/hash_police/version.rb
|
73
|
+
- spec/hash_police/check_result_spec.rb
|
74
|
+
- spec/hash_police/police_spec.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: a gem to check whether given to hashes are of the same format
|
99
|
+
test_files:
|
100
|
+
- spec/hash_police/check_result_spec.rb
|
101
|
+
- spec/hash_police/police_spec.rb
|