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

Classes: Step

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.



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

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.



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

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.



35
36
37
# File 'lib/transflow/transaction.rb', line 35

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)


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

def call(input, options = {})
  handler = handler_steps(options).map(&method(:step)).reduce(:>>)
  handler.call(input)
rescue StepError => err
  raise TransactionFailedError.new(self, err)
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)


74
75
76
77
78
79
80
81
# File 'lib/transflow/transaction.rb', line 74

def subscribe(listeners)
  if listeners.is_a?(Hash)
    listeners.each { |step, listener| steps[step].subscribe(listener) }
  else
    steps.each { |(_, step)| step.subscribe(listeners) }
  end
  self
end

#to_sString

Coerce a transaction into string representation

Returns:

  • (String)


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

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