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.



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

def initialized
  @initialized
end

Class Method Details

.board(passenger) ⇒ Object



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

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

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



102
103
104
# File 'lib/ellington/route.rb', line 102

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

.connectionsObject



98
99
100
# File 'lib/ellington/route.rb', line 98

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

.inherited(subclass) ⇒ Object



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

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

.initial_stateObject



88
89
90
# File 'lib/ellington/route.rb', line 88

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

.initialized?Boolean

Returns:

  • (Boolean)


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

def initialized?
  @initialized
end

.line_completed(line_info) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/ellington/route.rb', line 114

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



61
62
63
# File 'lib/ellington/route.rb', line 61

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

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



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

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

.passenger_attrs_to_logObject



106
107
108
# File 'lib/ellington/route.rb', line 106

def passenger_attrs_to_log
  @passenger_attrs_to_log ||= []
end

.set_passenger_attrs_to_log(*attrs) ⇒ Object



110
111
112
# File 'lib/ellington/route.rb', line 110

def set_passenger_attrs_to_log(*attrs)
  @passenger_attrs_to_log = attrs
end

.statesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ellington/route.rb', line 65

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

#create_passenger(context, ticket: Ellington::Ticket.new, state_history: []) ⇒ Object



38
39
40
41
42
# File 'lib/ellington/route.rb', line 38

def create_passenger(context, ticket: Ellington::Ticket.new, state_history: [])
  passenger = Passenger.new(context, route: self, ticket: ticket, state_history: state_history)
  passenger.current_state = initial_state
  passenger
end

#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