HCast
HCast is a library for casting hash attributes
Usage
Create caster class and declare hash attributes inside:
class ContactCaster
include HCast::Caster
attributes do
hash :contact do
string :name
integer :age, optional: true
float :weight
date :birthday
datetime :last_logged_in
time :last_visited_at
hash :company do
string :name
end
array :emails, each: :string
array :social_accounts, each: :hash do
string :name
symbol :type
end
end
end
end
Instanticate the caster and give your hash for casting:
ContactCaster.cast({
contact: {
name: "John Smith",
age: "22",
weight: "65.5",
birthday: "2014-02-02",
last_logged_in: "2014-02-02 10:10:00",
last_visited_at: "2014-02-02 10:10:00",
company: {
name: "MyCo"
},
emails: ["[email protected]", "[email protected]"],
social_accounts: [
{
name: "john_smith",
type: "twitter"
},
{
name: "John",
type: :facebook
}
]
}
}
})
The casted will cast your hash and output will be:
{
contact: {
name: "John Smith",
age: 22,
weight: 65.5,
birthday: #<Date: 2014-02-02 ((2456691j,0s,0n),+0s,2299161j)>,
last_logged_in: #<DateTime: 2014-02-02T10:10:00+00:00 ((2456691j,36600s,0n),+0s,2299161j)>,
last_visited_at: 2014-02-02 10:10:00 +0400,
company: {
name: "MyCo"
},
emails: ["[email protected]", "[email protected]"],
social_accounts: [
{
name: "john_smith",
type: :twitter"
},
{
name: "John",
type: :facebook
}
]
}
}
if some of the attributes can't be casted the HCast::Errors::CastingError will be raised
Installation
Add this line to your application's Gemfile:
gem 'hcast'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hcast
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request