Quack
Quack is a simple Ruby scalar type coercion library.
Installation
Add this line to your application's Gemfile:
gem 'quack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install quack
Usage
Quack is able to guess the following type categories and cast values to appropriate built-in type:
Integer
Float
Time
Boolean
Null
String
Integer
value = Quack("123")
value.class
#=> Quack::Types::Integer
value.to_coerced
#=> 123
value.to_coerced.class
#=> Integer
Integer (Ruby 2.3 and earlier)
value = Quack("123")
value.class
#=> Quack::Types::Integer
value.to_coerced
#=> 123
value.to_coerced.class
#=> Fixnum
Float
value = Quack("29.4")
value.class
#=> Quack::Types::Float
value.to_coerced
#=> 29.4
value.to_coerced.class
#=> Float
Time
value = Quack("2014-03-22T03:00:00Z")
value.class
#=> Quack::Types::Time
value.to_coerced
#=> 2014-03-22T03:00:00+00:00
value.to_coerced.class
#=> Time
Boolean
value = Quack("true")
value.class
#=> Quack::Types::Boolean
value.to_coerced
#=> true
value.to_coerced.class
#=> TrueClass
value = Quack("false")
value.class
#=> Quack::Types::Boolean
value.to_coerced
#=> false
value.to_coerced.class
#=> FalseClass
Null
value = Quack(nil)
value.class
#=> Quack::Types::Null
value.to_coerced
#=> nil
value.to_coerced.class
#=> NilClass
String
value = Quack("foo")
value.class
#=> Quack::Types::String
value.to_coerced
#=> foo
value.to_coerced.class
#=> String
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