Class: LittleGhost::Assembly::Trajectory

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/little_ghost/assembly_execution.rb

Overview

Immutable queries over the steps returned by one assembly invocation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Trajectory

Builds query indexes for steps.



133
134
135
136
137
# File 'lib/little_ghost/assembly_execution.rb', line 133

def initialize(steps)
  @steps = Array(steps).freeze
  @by_id = @steps.to_h { |step| [step.id, step] }.freeze
  freeze
end

Instance Attribute Details

#stepsObject (readonly)

Immutable steps in execution order.



130
131
132
# File 'lib/little_ghost/assembly_execution.rb', line 130

def steps
  @steps
end

Instance Method Details

#attempts_for(id) ⇒ Object

Returns attempts belonging to one step.



146
# File 'lib/little_ghost/assembly_execution.rb', line 146

def attempts_for(id) = step(id)&.attempts || [].freeze

#children(id) ⇒ Object

Returns steps whose parent is id.



144
145
# File 'lib/little_ghost/assembly_execution.rb', line 144

def children(id) = steps.select { |item| item.parent_id == id.to_s }.freeze
# Returns attempts belonging to one step.

#concurrent?(first_id, second_id) ⇒ Boolean

Indicates whether attempts from two steps overlapped in time.

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
166
# File 'lib/little_ghost/assembly_execution.rb', line 156

def concurrent?(first_id, second_id)
  first = step(first_id)
  second = step(second_id)
  return false unless first && second

  first.attempts.any? do |left|
    second.attempts.any? do |right|
      left.started_at < right.finished_at && right.started_at < left.finished_at
    end
  end
end

#each(&block) ⇒ Object

Iterates through steps in execution order.



140
141
# File 'lib/little_ghost/assembly_execution.rb', line 140

def each(&block) = steps.each(&block)
# Finds one step by its stable ID.

#step(id) ⇒ Object

Finds one step by its stable ID.



142
143
# File 'lib/little_ghost/assembly_execution.rb', line 142

def step(id) = @by_id[id.to_s]
# Returns steps whose parent is +id+.

#transitionsObject

Returns declared predecessor-to-step ID pairs.



149
150
151
152
153
# File 'lib/little_ghost/assembly_execution.rb', line 149

def transitions
  steps.flat_map do |item|
    item.predecessor_ids.map { |predecessor| [predecessor, item.id].freeze }
  end.freeze
end