Created
February 23, 2018 21:31
-
-
Save baldwindavid/8b0554b72ec65095ab3b4c0abf7d9d5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pipe | |
# Calls a list of methods in succession (i.e. pipes through) | |
# Include the module in your class to prove the `pipe` method. | |
# | |
# Example: | |
# | |
# include Pipe | |
# | |
# def do_a_thing | |
# pipe( | |
# "hello world", | |
# :first_method, | |
# :second_method, | |
# [SomeClass, :class_method_to_run_third] | |
# :last_method | |
# ) | |
# end | |
# | |
# @param start_value The value that is going to be piped through various methods. | |
# @param method_names [:symbol, [Class, :symbol]] The names of the methods | |
# to pipe the value through. These can be instance methods or class methods. | |
# Class methods should include the class name and method name in an array. | |
def pipe(start_value, *method_names) | |
method_names.inject(start_value) do |value, method_name| | |
case method_name | |
when Array | |
method_name[0].send(method_name[1], value) | |
else | |
send(method_name, value) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment