forked from lorint/refactored_tfl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation.rb
31 lines (27 loc) · 829 Bytes
/
station.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require './line'
class Station
attr_accessor :lines
attr_reader :name
def initialize(name)
@name = name
@lines = []
end
def neighbors
# Start with an empty array, and bring in for this station all of its lines
self.lines.inject([]) do |result, line|
# For this given line, find my position amongst all the stations.
index = line.stations.index(self)
# Add the station one less than me (as long as it's not the beginning)
result << line.stations[index - 1] if index > 0
# Add the station one more than me (as long as it's not the end)
result << line.stations[index + 1] if index < (line.stations.count - 1)
end
end
# Factory for building Station objects
def self.build_station(name, line)
station = Station.new(name)
station.lines = [line]
line.stations << station
station
end
end