Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/szhu/hashcontrol
Hashes, conveniently.
https://github.com/szhu/hashcontrol
Last synced: about 1 month ago
JSON representation
Hashes, conveniently.
- Host: GitHub
- URL: https://github.com/szhu/hashcontrol
- Owner: szhu
- License: mit
- Created: 2014-08-23T22:33:09.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-26T22:26:55.000Z (over 9 years ago)
- Last Synced: 2024-04-25T08:43:01.935Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 234 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
HashControl
===========[![Build Status](https://travis-ci.org/szhu/hashcontrol.svg?branch=master)](https://travis-ci.org/szhu/hashcontrol)
[![Code Climate](https://codeclimate.com/github/szhu/hashcontrol/badges/gpa.svg)](https://codeclimate.com/github/szhu/hashcontrol)```shell
gem install hash_control
```This Ruby library provides some conveniences for using and manipulating hash-like data.
## Features
`HashControl::Model` is a class with
- validation checking
- getting and setting properties with both `[]` and accessor methods
- setter methods can be omitted to prevent mutation
- [AwesomePrint](https://github.com/michaeldv/awesome_print) supportWant just a single-use validator? Use `HashControl::Validator`.
## Examples### Model
```ruby
require 'hash_control'
class Comment
include ::HashControl::Model
require_key :author, :body, :date
permit_key :image
endrequire 'hash_control'
class Something
include ::HashControl::Model
require_key :id
permit_all_keys
endComment.new(author: 'me', body: 'interesting stuff', date: Time.now)
Comment.new(body: "this ain't gonna fly")
# ArgumentError: extra params [:extra]
# in {:body=>"this ain't gonna fly", :author=>"me", :date=>2014-01-01 00:00:00 -0000, :extra=>"hullo"}Something.new(body: "this, however, will")
# ArgumentError: required params [:id] missing
# in {:body=>"this, however, will"}Something.new(id: 1, body: "oops my bad")
```### Validator
```ruby
require 'hash_control'
get_request = {
get: '/api/article/23/comment/34/show'
}
post_request = {
post: '/api/article/23/comment/34/update',
body: {
date: Time.new,
body: 'hullo',
meta: 'fsdkfsifhdsfsdhkj'
}
}
validator = ::HashControl::Validator.new(post_request)
validator.require(:post).permit(:body).only
# `permit` marks keys as allowed but doesn't do any verification
# `only` ensures no other keys are presentclass CustomValidator < ::HashControl::Validator
def validate_request
require_one_of(:get, :post)
enddef validate_get_request
validate_request.only
enddef validate_post_request
validate_request.permit(:body).only
end
endCustomValidator.new(get_request).validate_get_request
CustomValidator.new(post_request).validate_post_request
```## Credit
**HashControl** was originally designed and written by [@szhu] at [@IFTTT].
[@szhu]: https://github.com/szhu
[@IFTTT]: https://github.com/IFTTT