Last active
July 12, 2019 02:12
-
-
Save siassaj/4088eee239a82918ee01fc47a30d8bec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class DoThing | |
include Dry::Monads::Result::Mixin | |
include Dry::Monads::Do.for(:call) | |
Context = Struct.new(:user, :params, :accumulator) | |
Result = Struct.new(:output) | |
Error = Struct.new(:step, :errors) | |
def call(input) | |
context = yield railway(Context.new(input[:user], input[:params], {})) | |
context = yield set_one(context) | |
context = yield set_two(context) | |
result = Result.new(context.accumulator[:some_value]) | |
Success(result) | |
end | |
private | |
def set_one(context) | |
operation_one(context).bind do |v| | |
operation_two(v) | |
end do |m| | |
m.success { |result| Success(result) } | |
m.failure { |error| Success(recover_one(error)) } | |
end | |
end | |
def set_two(context) | |
operation_three(context).bind do |v| | |
operation_four(v) | |
end do |m| | |
m.success { |result| Success(result) } | |
m.failure { |error| Success(recover_two(error)) } | |
end | |
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