Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomekw/hash_assertions
Hash assertions: assert required keys, assert keys with values
https://github.com/tomekw/hash_assertions
Last synced: 4 days ago
JSON representation
Hash assertions: assert required keys, assert keys with values
- Host: GitHub
- URL: https://github.com/tomekw/hash_assertions
- Owner: tomekw
- License: mit
- Created: 2013-06-08T17:55:55.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-06-10T12:57:52.000Z (over 11 years ago)
- Last Synced: 2024-08-10T20:01:12.944Z (3 months ago)
- Language: Ruby
- Size: 121 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# hash_assertions
[![Build Status](https://travis-ci.org/tomekw/hash_assertions.png)](https://travis-ci.org/tomekw/hash_assertions)
Hash assertions: assert required keys, assert keys with values
## Installation
Add this line to your application's Gemfile:
``` ruby
gem "hash_assertions"
```And then execute:
``` sh
$ bundle
```Or install it yourself as:
``` sh
$ gem install hash_assertions
```and require it:
``` ruby
require "hash_assertions"
```## Usage
### assert_required_keys
``` ruby
require "hash_assertions/assert_required_keys"# Raises nothing
{ foo: "bar", baz: "qux" }.assert_required_keys(:foo, :baz)# Raises ArgumentError, "Missing required key: baz"
{ foo: "bar" }.assert_required_keys(:foo, :baz)# Raises ArgumentError, "Missing required key: baz"
{ foo: "bar", baz: "qux" }.assert_required_keys(:foo, "baz")# Raises ArgumentError, "Missing required key: [:foo, :baz]"
# (assert_required_keys doesn't flatten arguments!)
{ foo: "bar", baz: "qux" }.assert_required_keys([:foo, :baz])
```### assert_keys_with_values
``` ruby
require "hash_assertions/assert_keys_with_values"# Raises nothing
{ foo: "bar", baz: "qux" }.assert_keys_with_values(:foo, :baz)# Raises nothing
# (blank string or empty array is treated as valid value)
{ foo: "bar", baz: "" }.assert_keys_with_values(:foo, :baz)# Raises ArgumentError, "No value for key: baz"
{ foo: "bar", baz: nil }.assert_keys_with_values(:foo, :baz)# Raises ArgumentError, "No value for key: baz"
{ foo: "bar", baz: "qux" }.assert_keys_with_values(:foo, "baz")# Raises ArgumentError, "No value for key: [:foo, :baz]"
# (assert_keys_with_values doesn't flatten arguments!)
{ foo: "bar", baz: "qux" }.assert_keys_with_values([:foo, :baz])
```## 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