How to pass a value to step from class/instance scope?
Let's consider the following service:
class Service
# ...
step EscapeRegexp, \
in: {pattern: ???}, # `???` is just a placeholder here, it is not a valid Ruby syntax.
out: :escaped
# ...
end
We need to pass ENV["PATH_PATTERN"]
to the EscapeRegexp
step as the pattern
param.
How that can be achievable?
There are multiple ways:
-
Using
raw
.step EscapeRegexp, \
in: {pattern: raw(ENV["PATH_PATTERN"])},
out: :escapedThis is how
step
call is "translated" to regular service invocation under the hood:def step_result
@step_result ||= EscapeRegexp.result(pattern: ENV["PATH_PATTERN"])
endSince
raw
is a class method you can pass it anything from the enclosing class scope.The value is forwarded without any intermediate processing.
step EscapeRegexp, \
in: {pattern: raw(any_class_method)},
out: :escaped -
Using
proc
form.step EscapeRegexp, \
in: {pattern: -> { ENV["PATH_PATTERN"] }},
out: :escapedThe
proc
form does not additionally process the passed value as well, but it is evaluated in the instance scope.So you can access any service instance methods in it.
step EscapeRegexp, \
in: {pattern: -> { any_instance_method }},
out: :escaped
info
Check the Step to Result Translation table for more detailed reference.