class Ellington::Line

Attributes

route[RW]
route_class[RW]

Public Class Methods

goal(*states)
Alias for: pass_target
new() click to toggle source
# File lib/ellington/line.rb, line 33
def initialize
  if stations.empty?
    message = "#{self.class.name} has no stations!"
    raise Ellington::NoStationsDeclared.new(message)
  end

  if goal.empty?
    message = "#{self.class.name} has no goal!"
    raise Ellington::NoGoalDeclared.new(message)
  end
end
pass_target(*states) click to toggle source
# File lib/ellington/line.rb, line 14
def pass_target(*states)
  @goal ||= Ellington::Target.new(*states)
end
Also aliased as: passed, goal
passed(*states)
Alias for: pass_target
stations() click to toggle source
# File lib/ellington/line.rb, line 10
def stations
  @stations ||= Ellington::StationList.new(self)
end

Public Instance Methods

board(passenger) click to toggle source
# File lib/ellington/line.rb, line 49
def board(passenger)
  formula.run passenger
end
boarded?(passenger) click to toggle source
# File lib/ellington/line.rb, line 53
def boarded?(passenger)
  !(passenger.state_history & states.keys).empty?
end
formula() click to toggle source
# File lib/ellington/line.rb, line 61
def formula
  @formula ||= begin
    Hero::Formula[name]
    Hero::Formula[name].steps.clear
    stations.each do |station|
      Hero::Formula[name].add_step station
    end
    Hero::Formula[name]
  end
end
initial_states() click to toggle source
# File lib/ellington/line.rb, line 45
def initial_states
  stations.first.initial_states
end
name() click to toggle source
# File lib/ellington/line.rb, line 57
def name
  @name ||= "#{route_class.name} #{self.class.name}"
end
states() click to toggle source
# File lib/ellington/line.rb, line 72
def states
  @states ||= begin
    catalog = StateJacket::Catalog.new
    stations.each_with_index do |station, index|
      station.states.each do |state, transitions|
        catalog[state] = transitions.nil? ? nil : transitions.dup
      end

      if index < stations.length - 1
        next_station = stations[index + 1]
        catalog[station.passed] = next_station.states.keys
      end
    end
    catalog.lock
    catalog
  end
end
station_completed(station_info) click to toggle source
# File lib/ellington/line.rb, line 90
def station_completed(station_info)
  line_info = Ellington::LineInfo.new(self, station_info)
  if line_info.station == stations.last ||
    line_info.passenger.current_state == line_info.station.failed
      return complete_line(line_info)
  end

  log line_info.station_completed_message # TODO: add *passenger_attrs
end

Protected Instance Methods

complete_line(line_info) click to toggle source
# File lib/ellington/line.rb, line 102
def complete_line(line_info)
  log line_info.station_completed_message # TODO: add *passenger_attrs
  log line_info.line_completed_message # TODO: add *passenger_attrs
  changed
  notify_observers line_info
end
log(message) click to toggle source
# File lib/ellington/line.rb, line 109
def log(message)
  return unless Ellington.logger
  Ellington.logger.info message
end