dry-rb / dry-transaction

Business transaction DSL
http://dry-rb.org/gems/dry-transaction
MIT License
468 stars 55 forks source link

Allow for transaction steps without an input argument #59

Closed dNitza closed 7 years ago

dNitza commented 7 years ago

Currently, each step in a sequence is expected to have a single input argument. I ran into a scenario today where I didn't the first step to accept any arguments as its job was to prepare data for the next step in my transaction.

Below is the code for what I was aiming to achieve:

  t.define "main.transactions.my_transaction" do
    step :prepare_data, with: "main.operations.prepare_data"
    step :process_data, with: "main.operations.process_data"
  end
# prepare_data.rb
class PrepareData
  def call
     # prepare data here
     Right(data)
  end
end
# process_data.rb
class ProcessData
  def call(data)
    # do stuff with data here
  end
end

And then I would call it like so: Main::Container["main.transactions.my_transaction"].()

When I do this, I get the following error:

ArgumentError: wrong number of arguments (given 0, expected 1..2)
from /Users/daniel/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/dry-transaction-0.7.0/lib/dry/transaction/sequence.rb:45:in `call'

as dry-transaction is expecting an argument for the first step in the sequence.

To get around it I have defined the call method in PrepareData like so: def call(_) and am calling the transaction like so: Main::Container["main.transactions.my_transaction"].(nil)

Ideally we would be able to call a transaction without having to pass in an arg if nothing is required.

Thanks! ❤️

timriley commented 7 years ago

Yep, we should totally support this. Thanks for the report, @dNitza. We'll probably do it after the work in #58, which is what we're actively developing atm.

timriley commented 7 years ago

Fixed in #69