FirstOf
FirstOf can be used when multiple values can be reduced into a single return value. This is common in things like XML/JSON where the "intended" value is available in 2 fields and is determined by the presence of a value in the document and is often prioritized (first check field 1, then field 2, then field x)
FirstOf handles calling methods/lambdas and prioritizing them.
Installation
Add this line to your application's Gemfile:
gem 'first_of'
And then execute:
$ bundle
Or install it yourself as:
$ gem install first_of
Usage
require 'first_of'
class Widget
include ::FirstOf
def nothing; nil; end
def selfie; self; end
def something; "something"; end
end
= Widget.new
# Use it to get to something usable on self
.first_of(:nothing, :something, :self) # => "something"
.first_of(:nothing, :self, :something) # => self
.first_of(:nothing, :nothing, :something) # => "something"
# Wrap calls in an Array to `try_chain` them
.first_of([:self, :nothing], :something) # => "something"
# Wrap a call in a lambda (or any callable) to delay execution
.first_of(:something, lambda { "derp" }) # => "something"
.first_of(lambda { "derp" }, :something) # => "derp"
.first_of(lambda { nothing }, :something) # => "something"
# Prioritize the calls/call chains if desired
.first_of(2 => :something, 1 => lambda { "derp" }) # => "derp"
.first_of(2 => lambda { "derp" }, 1 => :something) # => "something"
.first_of(2 => lambda { nothing }, 1 => :something) # => "something"
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