Class: Map::Tube::Graph
- Inherits:
-
Object
- Object
- Map::Tube::Graph
- Defined in:
- lib/map/tube/graph.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#stations ⇒ Object
Returns the value of attribute stations.
Instance Method Summary collapse
- #add_line(line) ⇒ Object
- #add_station(station) ⇒ Object
- #get_line_by_id(line_id) ⇒ Object
- #get_line_by_name(line_name) ⇒ Object
- #get_shortest_route(from_station_name, to_station_name) ⇒ Object
- #get_station_by_id(station_id) ⇒ Object
- #get_station_by_name(station_name) ⇒ Object
- #graphviz ⇒ Object
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
6 7 8 9 |
# File 'lib/map/tube/graph.rb', line 6 def initialize @stations = [] @lines = [] end |
Instance Attribute Details
#lines ⇒ Object
Returns the value of attribute lines.
4 5 6 |
# File 'lib/map/tube/graph.rb', line 4 def lines @lines end |
#stations ⇒ Object
Returns the value of attribute stations.
4 5 6 |
# File 'lib/map/tube/graph.rb', line 4 def stations @stations end |
Instance Method Details
#add_line(line) ⇒ Object
15 16 17 |
# File 'lib/map/tube/graph.rb', line 15 def add_line(line) @lines << line end |
#add_station(station) ⇒ Object
11 12 13 |
# File 'lib/map/tube/graph.rb', line 11 def add_station(station) @stations << station end |
#get_line_by_id(line_id) ⇒ Object
33 34 35 |
# File 'lib/map/tube/graph.rb', line 33 def get_line_by_id(line_id) find_by(:line, :id, line_id) end |
#get_line_by_name(line_name) ⇒ Object
37 38 39 |
# File 'lib/map/tube/graph.rb', line 37 def get_line_by_name(line_name) find_by(:line, :name, line_name) end |
#get_shortest_route(from_station_name, to_station_name) ⇒ Object
19 20 21 22 23 |
# File 'lib/map/tube/graph.rb', line 19 def get_shortest_route(from_station_name, to_station_name) from_station = get_station_by_name(from_station_name) to_station = get_station_by_name(to_station_name) compute_shortest_path(from_station, to_station) end |
#get_station_by_id(station_id) ⇒ Object
25 26 27 |
# File 'lib/map/tube/graph.rb', line 25 def get_station_by_id(station_id) find_by(:station, :id, station_id) end |
#get_station_by_name(station_name) ⇒ Object
29 30 31 |
# File 'lib/map/tube/graph.rb', line 29 def get_station_by_name(station_name) find_by(:station, :name, station_name) end |
#graphviz ⇒ Object
53 54 55 |
# File 'lib/map/tube/graph.rb', line 53 def graphviz Graphviz.new(self) end |
#to_h ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/map/tube/graph.rb', line 41 def to_h {}.tap do |hash| @stations.each do |station| hash[station.name] = [] station.links.each do |link| curr_link = self.get_station_by_id(link) hash[station.name] << curr_link.name unless curr_link.name == station.name end end end end |