Skip to content

Instantly share code, notes, and snippets.

@pyrsmk
Last active July 2, 2020 20:01
Show Gist options
  • Save pyrsmk/cada07b4793269a882096bc974ed64e7 to your computer and use it in GitHub Desktop.
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 🤖
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