Method: Parser::Source::TreeRewriter#transaction

Defined in:
lib/parser/source/tree_rewriter.rb

#transactionObject

Provides a protected block where a sequence of multiple rewrite actions are handled atomically. If any of the actions failed by clobbering, all the actions are rolled back. Transactions can be nested.

Raises:

  • (RuntimeError)

    when no block is passed


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/parser/source/tree_rewriter.rb', line 310

def transaction
  unless block_given?
    raise "#{self.class}##{__method__} requires block"
  end

  previous = @in_transaction
  @in_transaction = true
  restore_root = @action_root

  yield

  restore_root = nil

  self
ensure
  @action_root = restore_root if restore_root
  @in_transaction = previous
end