Skip to content

Instantly share code, notes, and snippets.

@baldwindavid
Created February 23, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baldwindavid/8b0554b72ec65095ab3b4c0abf7d9d5e to your computer and use it in GitHub Desktop.
Save baldwindavid/8b0554b72ec65095ab3b4c0abf7d9d5e to your computer and use it in GitHub Desktop.
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