Class: Ellington::Line

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Observable
Includes:
HasTargets, Observable
Defined in:
lib/ellington/line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasTargets

#error_target, #fail_target, #state, #state_names

Constructor Details

#initializeLine

Returns a new instance of Line.



33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#routeObject

Returns the value of attribute route.



25
26
27
# File 'lib/ellington/line.rb', line 25

def route
  @route
end

#route_classObject

Returns the value of attribute route_class.



25
26
27
# File 'lib/ellington/line.rb', line 25

def route_class
  @route_class
end

Class Method Details

.pass_target(*states) ⇒ Object Also known as: passed, goal



14
15
16
# File 'lib/ellington/line.rb', line 14

def pass_target(*states)
  @goal ||= Ellington::Target.new(*states)
end

.stationsObject



10
11
12
# File 'lib/ellington/line.rb', line 10

def stations
  @stations ||= Ellington::StationList.new(self)
end

Instance Method Details

#board(passenger) ⇒ Object



49
50
51
# File 'lib/ellington/line.rb', line 49

def board(passenger)
  formula.run passenger
end

#boarded?(passenger) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ellington/line.rb', line 53

def boarded?(passenger)
  !(passenger.state_history & states.keys).empty?
end

#formulaObject



61
62
63
64
65
66
67
68
69
70
# 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_statesObject



45
46
47
# File 'lib/ellington/line.rb', line 45

def initial_states
  stations.first.initial_states
end

#nameObject



57
58
59
# File 'lib/ellington/line.rb', line 57

def name
  @name ||= "#{route_class.name} #{self.class.name}"
end

#statesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
# 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