Class: Transflow::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/transflow/transaction.rb

Overview

Transaction encapsulates calling individual steps registered within a transflow constructor.

It’s responsible for calling steps in the right order and optionally currying arguments for specific steps.

Furthermore you can subscribe event listeners to individual steps within a transaction.

Defined Under Namespace

Modules: Registry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Transaction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Transaction.



48
49
50
51
# File 'lib/transflow/transaction.rb', line 48

def initialize(steps)
  @steps = steps
  @step_names = steps.keys.reverse
end

Instance Attribute Details

#step_namesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/transflow/transaction.rb', line 45

def step_names
  @step_names
end

#stepsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/transflow/transaction.rb', line 40

def steps
  @steps
end

Instance Method Details

#call(input, options = {}) ⇒ Object Also known as: []

Call the transaction

Once transaction is called it will call the first step and its result will be passed to the second step and so on.

Examples:

my_container = {
  add_one: -> i { i + 1 },
  add_two: -> j { j + 2 }
}

transaction = Transflow(container: my_container) {
  step(:one, with: :add_one) { step(:two, with: :add_two) }
}

transaction.call(1) # 4

Parameters:

  • input (Object)

    The input for the first step

  • options (Hash) (defaults to: {})

    The curry-args map, optional

Returns:

  • (Object)


110
111
112
113
114
115
# File 'lib/transflow/transaction.rb', line 110

def call(input, options = {})
  handler = handler_steps(options).map(&method(:fn)).reduce(:>>)
  handler.call(input)
rescue Transproc::MalformedInputError => err
  raise TransactionFailedError.new(self, err.original_error)
end

#subscribe(listeners) ⇒ self

Subscribe event listeners to specific steps

Examples:

transaction = Transflow(container: my_container) {
  step(:one) { step(:two, publish: true }
}

class MyListener
  def self.two_success(*args)
    puts 'yes!'
  end

  def self.two_failure(*args)
    puts 'oh noez!'
  end
end

transaction.subscribe(two: my_listener)

transaction.call(some_input)

Parameters:

  • listeners (Hash<Symbol => Object>)

    The step=>listener map

Returns:

  • (self)


79
80
81
82
# File 'lib/transflow/transaction.rb', line 79

def subscribe(listeners)
  listeners.each { |step, listener| steps[step].subscribe(listener) }
  self
end

#to_sString

Coerce a transaction into string representation

Returns:

  • (String)


123
124
125
# File 'lib/transflow/transaction.rb', line 123

def to_s
  "Transaction(#{step_names.join(' => ')})"
end