Class: Trailblazer::Circuit::Activity

Inherits:
Struct
  • Object
show all
Defined in:
lib/trailblazer/circuit/activity.rb,
lib/trailblazer/circuit/alter.rb

Overview

This is a code structure to encapsulate the circuit execution behavior and the start, intermediate and end events, within a “physical” business process.

activity[:Start]
activity.()
activity.values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#circuitObject

Returns the value of attribute circuit

Returns:

  • (Object)

    the current value of circuit



9
10
11
# File 'lib/trailblazer/circuit/activity.rb', line 9

def circuit
  @circuit
end

#eventsObject

Returns the value of attribute events

Returns:

  • (Object)

    the current value of events



9
10
11
# File 'lib/trailblazer/circuit/activity.rb', line 9

def events
  @events
end

Class Method Details

.Before(activity, old_task, new_task, direction: raise, **kws) ⇒ Object

Find all ‘direction` connections TO <old_task> and rewire them to new_task, then connect new to old with `direction`.



5
6
7
8
9
10
11
12
13
# File 'lib/trailblazer/circuit/alter.rb', line 5

def self.Before(activity, old_task, new_task, direction:raise, **kws)
  Rewrite(activity, **kws) do |new_map|
    cfg = new_map.find_all { |act, outputs| outputs[direction]==old_task }
    # rewire old line to new task.
    cfg.each { |(activity, outputs)| outputs[direction] = new_task }
    # connect new_task --> old_task.
    new_map[new_task] = { direction => old_task }
  end
end

.Connect(activity, from, to, direction: raise) ⇒ Object



15
16
17
18
19
# File 'lib/trailblazer/circuit/alter.rb', line 15

def self.Connect(activity, from, to, direction:raise)
  Rewrite(activity) do |new_map|
    new_map[from][direction] = to
  end
end

.Rewrite(activity, debug: {}, events: {}) ⇒ Object

Deep-clones an Activity’s circuit and allows to alter its map by yielding it.

activity = Circuit::Activity::Rewrite(activity) do |map, evt|
  map[some_task] = { Circuit::Right => evt[:End] }
end

You can only add events as they might already be in use in the existing map.

:private:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/trailblazer/circuit/alter.rb', line 30

def self.Rewrite(activity, debug:{}, events:{})
  # decompose Activity and Circuit.
  circuit, original_events = activity.values
  map, end_events, original_debug  = circuit.to_fields

  original_events = original_events.to_h
  events.each { |k, v| original_events[k] = (original_events[k] || {}).merge(v) }

  # events = events.to_h.merge(added_events) # add new events.
  debug  = original_debug.to_h.merge(debug) # add new events.

  new_map = {} # deep-dup.
  map.each { |act, outputs| new_map[act] = outputs.dup }

  # recompose to an Activity.
  # new_map is mutable.
  Circuit::Activity(debug, original_events) { |evts| yield(new_map, evts); new_map }
end

Instance Method Details

#[](*args) ⇒ Object



10
11
12
# File 'lib/trailblazer/circuit/activity.rb', line 10

def [](*args)
  events[*args]
end

#call(*args, &block) ⇒ Object



14
15
16
# File 'lib/trailblazer/circuit/activity.rb', line 14

def call(*args, &block)
  circuit.(*args, &block)
end