Class: Ellington::Route

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
HasTargets, Observable
Defined in:
lib/ellington/route.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasTargets

error_target, fail_target, state, state_names

Constructor Details

#initializeRoute

Returns a new instance of Route.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ellington/route.rb', line 10

def initialize
  super self.class

  if lines.empty?
    message = "#{self.class.name} has no lines!"
    raise Ellington::NoLinesDeclared.new(message)
  end

  if goal.empty?
    message = "#{self.class.name} has no lines!"
    raise Ellington::NoGoalDeclared.new(message)
  end

  initialize_lines
  states
end

Class Attribute Details

.initializedObject (readonly)

Returns the value of attribute initialized.



41
42
43
# File 'lib/ellington/route.rb', line 41

def initialized
  @initialized
end

Class Method Details

.board(passenger) ⇒ Object



51
52
53
# File 'lib/ellington/route.rb', line 51

def board(passenger)
  lines.first.board passenger
end

.connect(line, after: [], strict: false) ⇒ Object



96
97
98
# File 'lib/ellington/route.rb', line 96

def connect(line, after: [], strict: false)
  connections << Ellington::Connection.new(line, *after, strict: false)
end

.connectionsObject



92
93
94
# File 'lib/ellington/route.rb', line 92

def connections
  @connections ||= Ellington::ConnectionList.new
end

.inherited(subclass) ⇒ Object



43
44
45
# File 'lib/ellington/route.rb', line 43

def inherited(subclass)
  (@subclasses ||= []) << subclass
end

.initial_stateObject



82
83
84
# File 'lib/ellington/route.rb', line 82

def initial_state
  @initial_state ||= "PRE #{name}"
end

.initialized?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ellington/route.rb', line 47

def initialized?
  @initialized
end

.line_completed(line_info) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/ellington/route.rb', line 108

def line_completed(line_info)
  route_info = Ellington::RouteInfo.new(self, line_info)
  connections = required_connections(route_info.passenger)
  return complete_route(route_info) if connections.empty?
  connections.each do |connection|
    connection.line.board route_info.passenger
  end
end

.linesObject



55
56
57
# File 'lib/ellington/route.rb', line 55

def lines
  @lines ||= Ellington::LineList.new(self)
end

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



86
87
88
# File 'lib/ellington/route.rb', line 86

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

.passenger_attrs_to_logObject



100
101
102
# File 'lib/ellington/route.rb', line 100

def passenger_attrs_to_log
  @passenger_attrs_to_log ||= []
end

.set_passenger_attrs_to_log(*attrs) ⇒ Object



104
105
106
# File 'lib/ellington/route.rb', line 104

def set_passenger_attrs_to_log(*attrs)
  @passenger_attrs_to_log = attrs
end

.statesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ellington/route.rb', line 59

def states
  @states ||= begin
    catalog = StateJacket::Catalog.new
    catalog.add initial_state => lines.first.stations.first.states.keys

    lines.each do |line|
      line.states.each do |state, transitions|
        catalog[state] = transitions.nil? ? nil : transitions.dup
      end
    end

    connections.each do |connection|
      connection.states.each do |state|
        catalog[state] ||= []
        catalog[state].concat connection.line.stations.first.states.keys
      end
    end

    catalog.lock
    catalog
  end
end

Instance Method Details

#initialize_linesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/ellington/route.rb', line 27

def initialize_lines
  lines.each do |line|
    line.route = self
    line.add_observer self, :line_completed
    line.stations.each do |station|
      station.line = line
      station.add_observer line, :station_completed
    end
  end
end