Last active
July 2, 2020 20:01
-
-
Save pyrsmk/cada07b4793269a882096bc974ed64e7 to your computer and use it in GitHub Desktop.
Example of a pipe implementation in a service, from the functional programming world 🤖
This file contains 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
class SomeService | |
def call(some_value, bypass_some_method4 = false) | |
pipe( | |
some_value, | |
:some_method1, | |
:some_method2, | |
-> (value) { value * 2 }, | |
bypass_some_method4 ? :noop : :some_method4, | |
:some_method5 | |
) | |
end | |
private def pipe(initial, *methods) | |
methods.reduce(initial) do |value, obj| | |
case obj | |
when Symbol | |
method(obj).call(value) | |
when Proc | |
obj.call(value) | |
end | |
end | |
end | |
private def noop(value) | |
value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment