A library for humazing and sanitizing Array
and Hash
Add this line to your application's Gemfile:
gem 'type-humanizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install type-humanizer
First, lets humanize some data
We can humanize an Array
and a Hash
To do that, we just call Humanizer::Human#from
Humanizer::Human.from ['a', 'b', 'c']
=> "a, b, c"
Humanizer::Human.from { :foo => 'bar', :boo => 'baz' }
=> "foo: bar, boo: baz"
Now lets sanitize it
Again, we can sanitize an Array
and a Hash
For this we need the Humanizer::Sanitize
class
sanitizer = Humanizer::Sanitize.new
sanitizer.to_array 'a, b, c'
=> ["a", "b", "c"]
sanitizer.to_hash 'foo: bar, boo: baz'
=> { "foo" => "bar", "boo" => "baz" }
We can also sanitize a hash of params.. This could be some Rails params
All we need to do is pass the params and specify which params should be sanitized to what type
params = { friends: 'Jack, Jill', favorites: 'drink: Coffee, fruit: Banana' }
Humanizer::Sanitize.params params, friends: :array, favorites: :hash
=> { :friends => ["Jack", "Jill"], :favorites => { "drink" => "Coffee", "fruit" => "Banana" } }
You can configure the delimeters by creating an initializer:
Humanizer.configure do |config|
# These are the default values
config.array_delimeter = ', '
config.hash_delimeter = ', '
config.key_val_delimeter = ': '
end
- Fork it ( https://github.com/askehansen/humanizer/fork )
- 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 a new Pull Request