Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/derrickreimer/quack
A flexible type coercion library for Ruby
https://github.com/derrickreimer/quack
Last synced: 5 days ago
JSON representation
A flexible type coercion library for Ruby
- Host: GitHub
- URL: https://github.com/derrickreimer/quack
- Owner: derrickreimer
- License: mit
- Created: 2014-03-16T23:06:54.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-08-28T18:52:51.000Z (over 7 years ago)
- Last Synced: 2024-11-29T05:42:16.727Z (23 days ago)
- Language: Ruby
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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
```ruby
value = Quack("123")
value.class
#=> Quack::Types::Integer
value.to_coerced
#=> 123
value.to_coerced.class
#=> Integer
```### Integer (Ruby 2.3 and earlier)
```ruby
value = Quack("123")
value.class
#=> Quack::Types::Integer
value.to_coerced
#=> 123
value.to_coerced.class
#=> Fixnum
```### Float
```ruby
value = Quack("29.4")
value.class
#=> Quack::Types::Float
value.to_coerced
#=> 29.4
value.to_coerced.class
#=> Float
```### Time
```ruby
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
```ruby
value = Quack("true")
value.class
#=> Quack::Types::Boolean
value.to_coerced
#=> true
value.to_coerced.class
#=> TrueClassvalue = Quack("false")
value.class
#=> Quack::Types::Boolean
value.to_coerced
#=> false
value.to_coerced.class
#=> FalseClass
```### Null
```ruby
value = Quack(nil)
value.class
#=> Quack::Types::Null
value.to_coerced
#=> nil
value.to_coerced.class
#=> NilClass
```### String
```ruby
value = Quack("foo")
value.class
#=> Quack::Types::String
value.to_coerced
#=> foo
value.to_coerced.class
#=> String
```## 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