Skip to content

Instantly share code, notes, and snippets.

@siassaj
Last active July 12, 2019 02:12
Show Gist options
  • Save siassaj/d6ed898578628e09386143f806960543 to your computer and use it in GitHub Desktop.
Save siassaj/d6ed898578628e09386143f806960543 to your computer and use it in GitHub Desktop.
class DoThing
include Dry::Transaction
Context = Struct.new(:user, :params, :accumulator)
Result = Struct.new(:output)
Error = Struct.new(:step, :errors)
step :build_context
step :operation_one
step :operation_two
failure :recover_one
step :operation_three
step :operation_four
failure :recover_two
step :prepare_result
private
def build_context(input)
Success(Context.new(input[:user], input[:params], {}))
end
def prepare_result(context)
Success(Result.new(context.accumulator[:output]))
end
def operation_one(context)
# ...
end
def operation_two(context)
# ...
end
def operation_three(context)
# ...
end
def operation_four(context)
# ...
end
def recover_one(error)
# Success() with some recovery
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment