Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mz026/hash_police
a gem to check whether given two hashes are of the same format
https://github.com/mz026/hash_police
Last synced: 3 months ago
JSON representation
a gem to check whether given two hashes are of the same format
- Host: GitHub
- URL: https://github.com/mz026/hash_police
- Owner: mz026
- License: mit
- Created: 2014-01-13T09:48:24.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-06-19T04:46:55.000Z (over 6 years ago)
- Last Synced: 2024-05-12T05:46:24.464Z (6 months ago)
- Language: Ruby
- Homepage: https://rubygems.org/gems/hash_police/versions/0.0.4
- Size: 21.5 KB
- Stars: 34
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/mz026/hash_police.svg?branch=master)](https://travis-ci.org/mz026/hash_police)
[![Code Climate](https://codeclimate.com/github/mz026/hash_police/badges/gpa.svg)](https://codeclimate.com/github/mz026/hash_police)# HashPolice
A gem to check whether given to hashes are of the same format## Installation
Add this line to your application's Gemfile:
gem 'hash_police'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_police
## Usage
```ruby
rule = {
:name => "a string",
:age => 28,
:favorites => [ "a string" ],
:locations => [
{ :name => "string", :duration => 3 }
]
}valid = {
:name => "Jack",
:age => 28,
:favorites => [ "sport", "music" ],
:locations => [
{ :name => "Taiwan", :duration => 25 },
{ :name => "US", :duration => 5 }
]
}invalid = {
:name => [],
:age => "not a number",
:locations => [
{ :name => "Taiwan", :duration => 25 },
{ :name => 23 }
]
}police = HashPolice::Police.new(rule)
result = police.check(valid)
result.passed? # => true
result.error_messages # => ""result = police.check(invalid)
result.passed? # => false
result.error_messages #=> "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"
```## RSpec matcher:
`HashPolice` provides a RSpec matcher `have_the_same_hash_format_as` checking the formats of two hashes.
```ruby
require 'hash_police/rspec_matcher'it "should be able to use the matcher `have_the_same_hash_format_as`" do
expected = { 'str' => '', 'an_arr' => [ 1 ] }
to_be_checked = { 'str' => 'hola', :an_arr => [1,3,4] }expect(to_be_checked).to have_the_same_hash_format_as(expected)
end
```## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request