Created
August 8, 2017 06:19
-
-
Save prikha/71b90b1a264a3951a2b7033cd0b53f5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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