Nifty::Variants
If you are familiar with variants from a nice language like Haskell try this.
If you don't know what variants are start here
NOTE Nifty::Variants makes use of Refinements which weren't introduced until Ruby 2.1 and as a result no Ruby below 2.1 is supported. You're welcome to write a fancy shim, but I don't recommend it.
Installation
Add this line to your application's Gemfile:
gem 'nifty-variants'
And then execute:
$ bundle install -j2
Usage
nifty-variants
makes use of Refinements which were introduced in Ruby 2.1.
In order to use it in your class or module simply use it.
class OrderDispatch
using Nifty::Variants
def shipit!(order)
cases order,
digital: ->(o) { o.email! },
delivery: ->(o) { o.deliver! },
else: ->(o) { move_to_error_queue(o) }
end
end
Contributing
- Fork it ( https://github.com/[my-github-username]/nifty-variants/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