class RailwayOperation::Operation

This is the value object that holds the information necessary to run an operation

Attributes

name[R]
operation_surrounds[RW]
step_surrounds[RW]
track_alias[R]

Public Class Methods

format_name(op_or_name) click to toggle source
# File lib/railway_operation/operation.rb, line 19
def self.format_name(op_or_name)
  case op_or_name
  when Operation
    op_or_name.name
  when String, Symbol
    op_or_name.to_s.gsub(/\s+/, '_').downcase.to_sym
  else
    raise 'invalid operation name'
  end
end
new(operation_or_name) click to toggle source
Calls superclass method
# File lib/railway_operation/operation.rb, line 14
def self.new(operation_or_name)
  return operation_or_name if operation_or_name.is_a?(Operation)
  super
end
new(name) click to toggle source
# File lib/railway_operation/operation.rb, line 30
def initialize(name)
  @name = self.class.format_name(name)
  @operation_surrounds = []
  @step_surrounds = Generic::EnsuredAccess.new({}) { StepsArray.new }
  @track_alias = [noop_track]
  @tracks = Generic::FilledMatrix.new(row_type: StepsArray)
end

Public Instance Methods

[](track_identifier, step_index = nil) click to toggle source
# File lib/railway_operation/operation.rb, line 38
def [](track_identifier, step_index = nil)
  tracks[
    track_index(track_identifier),
    step_index
  ]
end
[]=(track_identifier, step_index, step) click to toggle source
# File lib/railway_operation/operation.rb, line 45
def []=(track_identifier, step_index, step)
  tracks[
    track_index(track_identifier),
    step_index
  ] = step
end
add_step(track_identifier, method = nil, &block) click to toggle source
# File lib/railway_operation/operation.rb, line 52
def add_step(track_identifier, method = nil, &block)
  self[track_identifier, last_step_index + 1] = block || method
end
initial_track() click to toggle source
# File lib/railway_operation/operation.rb, line 103
def initial_track
  track_identifier(1)
end
last_step_index() click to toggle source
# File lib/railway_operation/operation.rb, line 70
def last_step_index
  tracks.max_column_index
end
noop_track() click to toggle source
# File lib/railway_operation/operation.rb, line 99
def noop_track
  :noop_track
end
stepper_function(fn = nil, &block) click to toggle source
# File lib/railway_operation/operation.rb, line 56
def stepper_function(fn = nil, &block)
  @stepper_function ||= fn || block
end
strategy(tracks, stepper_fn) click to toggle source
# File lib/railway_operation/operation.rb, line 65
def strategy(tracks, stepper_fn)
  tracks(*tracks)
  stepper_function(stepper_fn)
end
successor_track(track_id) click to toggle source
# File lib/railway_operation/operation.rb, line 74
def successor_track(track_id)
  next_index = track_index(track_id) + 1
  return if tracks.count <= next_index

  if track_id.is_a?(Numeric)
    next_index
  else
    track_identifier(next_index)
  end
end
track_identifier(index_or_id) click to toggle source
# File lib/railway_operation/operation.rb, line 85
def track_identifier(index_or_id)
  return index_or_id unless index_or_id.is_a?(Integer)
  validate_index(index_or_id)

  @track_alias[index_or_id] || index_or_id
end
track_index(track_identifier) click to toggle source
# File lib/railway_operation/operation.rb, line 92
def track_index(track_identifier)
  index = @track_alias.index(track_identifier) || track_identifier
  validate_index(index)

  index
end
tracks(*names) click to toggle source
# File lib/railway_operation/operation.rb, line 60
def tracks(*names)
  return @tracks if names.empty?
  @track_alias = [noop_track, *names]
end

Private Instance Methods

validate_index(index) click to toggle source
# File lib/railway_operation/operation.rb, line 109
def validate_index(index)
  unless index.is_a?(Integer) && index.positive?
    raise "Invalid track `#{index}`, must be a positive integer"
  end
end