Skip to content

Instantly share code, notes, and snippets.

@dmaze
Created November 14, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaze/ea78510a8870089be510a55d8fb38a8e to your computer and use it in GitHub Desktop.
Save dmaze/ea78510a8870089be510a55d8fb38a8e to your computer and use it in GitHub Desktop.
#!/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