Created
November 14, 2018 21:11
-
-
Save dmaze/ea78510a8870089be510a55d8fb38a8e 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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'dry/container' | |
require 'dry/transaction' | |
require 'dry/transaction/operation' | |
# Run this as an around step | |
class First | |
# Running this transaction step will fail with | |
# LocalJumpError: yield called out of block | |
# But if you comment out the first include and uncomment the | |
# second it works | |
include Dry::Transaction::Operation | |
# include Dry::Monads::Result::Mixin | |
def call(input) | |
puts "first #{input}" | |
yield Success(input + 1) | |
ensure | |
puts 'done with first' | |
end | |
end | |
# Run this as a normal step | |
class Second | |
include Dry::Transaction::Operation | |
def call(input) | |
puts "second #{input}" | |
Success(input + 1) | |
end | |
end | |
Container = Dry::Container.new | |
Container.register(:first) { First.new } | |
Container.register(:second) { Second.new } | |
# top-level transaction | |
class Transaction | |
include Dry::Transaction(container: Container) | |
around :first, with: 'first' | |
step :second, with: 'second' | |
end | |
result = Transaction.new.call(1) | |
puts(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment