Skip to content

Instantly share code, notes, and snippets.

@prikha
Created August 8, 2017 06:19
Show Gist options
  • Save prikha/71b90b1a264a3951a2b7033cd0b53f5e to your computer and use it in GitHub Desktop.
Save prikha/71b90b1a264a3951a2b7033cd0b53f5e to your computer and use it in GitHub Desktop.
require "dry/transaction"
class CreateUser
include Dry::Transaction
# step without rollback should not fail the whole process
# its optional
step :process
step :track, rollback: :rollback_track
step :persist
def process(input)
Right(name: input["name"], email: input["email"])
end
def track(input)
if input[:tracked]
DB << input[:email]
else
Right(input)
end
end
def rollback_track(input)
DB.delete(input[:email])
# simply pass it down for further rollbacks
Left(input)
end
def persist(input)
if DB << input
Right(input)
else
# Here we might control the value being sent to rollback operation/method
Left(input)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment