What is a step proc input?
A step proc input, also called an inline step input, evaluates a
Procin the organizer's instance scope at run time and passes the returned value to the step. It is written as{step_parameter_name: -> { ... }}inin:.class CalculateDoubled include ConvenientService::Standard::Config attr_reader :number def initialize(number:) @number = number end def result success(doubled: number * 2) end end class AwardLoyaltyPoints include ConvenientService::Standard::Config step CalculateDoubled, in: {number: -> { base_points * multiplier }}, out: {doubled: :points} private def base_points 7 end def multiplier 3 end end result = AwardLoyaltyPoints.result result.success? # => true result.data[:points] # => 42The proc is evaluated via
instance_execon the organizer, so it has access to the organizer's instance methods and state - here,base_pointsandmultiplierinside the lambda refer toAwardLoyaltyPoints's own private methods.Both
-> { ... }andproc { ... }work - anyProcis accepted.Like a step raw input, a step proc input does not go through any additional processing - the only difference is the scope it is evaluated in: instance scope for a proc input, class scope for a raw input.
Use a step proc input when a step needs a value computed at run time from the organizer's own methods, constants, or state, without extracting that computation into its own organizer method first - here,
base_points * multiplieris written inline at the step declaration instead of behind a dedicated method that a usual step input or a step alias input would have to reference.
See also
- What step input types are available?
- What is a usual step input?
- What is a step alias input?
- What is a step raw input?
- What is a step?