Class: Umwelt::History::Trace

Inherits:
Object
  • Object
show all
Includes:
Hanami::Interactor
Defined in:
lib/umwelt/history/trace.rb

Instance Method Summary collapse

Constructor Details

#initializeTrace

Returns a new instance of Trace.



9
10
11
12
13
14
# File 'lib/umwelt/history/trace.rb', line 9

def initialize
  @queue = Queue.new # for phases
  @phases_index = {}
  @continuity = []
  @timeline = {}
end

Instance Method Details

#call(history, phase_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/umwelt/history/trace.rb', line 16

def call(history, phase_id)
  to_index(history.phases)

  @queue.push from_index(phase_id)

  loop do
    break if @queue.empty?

    process(@queue.pop)
  end

  @continuity = @timeline.values
end

#enqueue(phase) ⇒ Object



35
36
37
38
# File 'lib/umwelt/history/trace.rb', line 35

def enqueue(phase)
  @queue.push from_index(phase.merge_id) unless phase.merge_id.nil?
  @queue.push from_index(phase.parent_id) unless phase.parent_id.nil?
end

#from_index(phase_id) ⇒ Object



40
41
42
43
# File 'lib/umwelt/history/trace.rb', line 40

def from_index(phase_id)
  phase = @phases_index[phase_id]
  phase || error!("Phase with ID #{phase_id} not exist, but referenced")
end

#process(phase) ⇒ Object



30
31
32
33
# File 'lib/umwelt/history/trace.rb', line 30

def process(phase)
  @timeline[phase[:id]] = phase
  enqueue(phase)
end

#to_index(phases) ⇒ Object



45
46
47
48
49
# File 'lib/umwelt/history/trace.rb', line 45

def to_index(phases)
  phases.each do |phase|
    @phases_index[phase[:id]] = phase
  end
end