Class: Metrorail::Line
- Inherits:
-
Object
- Object
- Metrorail::Line
- Defined in:
- lib/metrorail.rb
Overview
A class representing a line in the Metrorail system.
Constant Summary collapse
- @@lines =
[]
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stations ⇒ Object
readonly
Returns the value of attribute stations.
Class Method Summary collapse
- .all(force = false) ⇒ Object
- .find_by_id(id) ⇒ Object
- .update_cache ⇒ Object
- .update_cache_if_needed ⇒ Object
Instance Method Summary collapse
- #add_station(station) ⇒ Object
-
#initialize(id, name, stations = []) ⇒ Line
constructor
A new instance of Line.
Constructor Details
#initialize(id, name, stations = []) ⇒ Line
Returns a new instance of Line.
113 114 115 116 117 |
# File 'lib/metrorail.rb', line 113 def initialize(id, name, stations=[]) @id = id @name = name @stations = [] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
110 111 112 |
# File 'lib/metrorail.rb', line 110 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
110 111 112 |
# File 'lib/metrorail.rb', line 110 def name @name end |
#stations ⇒ Object (readonly)
Returns the value of attribute stations.
110 111 112 |
# File 'lib/metrorail.rb', line 110 def stations @stations end |
Class Method Details
.all(force = false) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/metrorail.rb', line 123 def self.all(force=false) if @@lines.size > 0 return @@lines else lines_request = Metrorail::make_request("Rail", "jLines")["Lines"] lines = [] lines_request.each do |line| new_line = Line.new(line["LineCode"], line["DisplayName"]) stations = Metrorail::make_request("Rail", "jPath", {fromStationCode: line["StartStationCode"], toStationCode: line["EndStationCode"]})["Path"] stations.each do |station| new_line.add_station(Metrorail::Station.find_by_id(station["StationCode"])) end lines << new_line end return lines end end |
.find_by_id(id) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/metrorail.rb', line 141 def self.find_by_id(id) self.update_cache_if_needed matching = self.all.select { |line| line.id == id } if matching.size > 0 matching[0] else nil end end |
.update_cache ⇒ Object
151 152 153 |
# File 'lib/metrorail.rb', line 151 def self.update_cache @@lines = self.all(true) end |
.update_cache_if_needed ⇒ Object
155 156 157 158 159 |
# File 'lib/metrorail.rb', line 155 def self.update_cache_if_needed if @@lines.size == 0 self.update_cache end end |
Instance Method Details
#add_station(station) ⇒ Object
119 120 121 |
# File 'lib/metrorail.rb', line 119 def add_station(station) @stations << station end |