Class: MetraSchedule::Train
- Inherits:
-
Object
- Object
- MetraSchedule::Train
- Defined in:
- lib/metra/train.rb
Instance Attribute Summary collapse
-
#bike_limit ⇒ Object
readonly
Returns the value of attribute bike_limit.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#my_arrival ⇒ Object
Injected when the line is filtered.
-
#my_departure ⇒ Object
Injected when the line is filtered.
-
#schedule ⇒ Object
readonly
Returns the value of attribute schedule.
-
#stops ⇒ Object
readonly
Returns the value of attribute stops.
-
#train_num ⇒ Object
readonly
Returns the value of attribute train_num.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #departure_and_arrival(start, destination) ⇒ Object
- #has_stop?(stop) ⇒ Boolean
- #in_time?(station, time) ⇒ Boolean
-
#initialize(options = {}) ⇒ Train
constructor
A new instance of Train.
- #my_travel_time ⇒ Object
- #print_my_travel_time ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Train
Returns a new instance of Train.
6 7 8 9 10 11 12 13 14 |
# File 'lib/metra/train.rb', line 6 def initialize(={}) unless .empty? @train_num = [:train_num] if .has_key?(:train_num) @schedule = [:schedule] if .has_key?(:schedule) @bike_limit = [:bike_limit] if .has_key?(:bike_limit) @direction = [:direction] if .has_key?(:direction) @stops = [:stops] if .has_key?(:stops) end end |
Instance Attribute Details
#bike_limit ⇒ Object (readonly)
Returns the value of attribute bike_limit.
3 4 5 |
# File 'lib/metra/train.rb', line 3 def bike_limit @bike_limit end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
3 4 5 |
# File 'lib/metra/train.rb', line 3 def direction @direction end |
#my_arrival ⇒ Object
Injected when the line is filtered
4 5 6 |
# File 'lib/metra/train.rb', line 4 def my_arrival @my_arrival end |
#my_departure ⇒ Object
Injected when the line is filtered
4 5 6 |
# File 'lib/metra/train.rb', line 4 def my_departure @my_departure end |
#schedule ⇒ Object (readonly)
Returns the value of attribute schedule.
3 4 5 |
# File 'lib/metra/train.rb', line 3 def schedule @schedule end |
#stops ⇒ Object (readonly)
Returns the value of attribute stops.
3 4 5 |
# File 'lib/metra/train.rb', line 3 def stops @stops end |
#train_num ⇒ Object (readonly)
Returns the value of attribute train_num.
3 4 5 |
# File 'lib/metra/train.rb', line 3 def train_num @train_num end |
Instance Method Details
#<=>(other) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/metra/train.rb', line 55 def <=>(other) if my_departure return 1 if self.my_departure > other.my_departure return -1 if self.my_departure < other.my_departure return 0 if self.my_departure == other.my_departure elsif @stops.first and other.stops.first return 1 if @stops.first.time > other.stops.first.time return -1 if @stops.first.time < other.stops.first.time return 0 if @stops.first.time == other.stops.first.time else 0 end end |
#departure_and_arrival(start, destination) ⇒ Object
36 37 38 39 40 |
# File 'lib/metra/train.rb', line 36 def departure_and_arrival(start, destination) departure = @stops.find {|s| s.station == start}.time arrival = @stops.find {|s| s.station == destination}.time {:departure => departure, :arrival => arrival} end |
#has_stop?(stop) ⇒ Boolean
16 17 18 19 |
# File 'lib/metra/train.rb', line 16 def has_stop?(stop) return true if stops.any? {|s| s.station == stop} false end |
#in_time?(station, time) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/metra/train.rb', line 21 def in_time?(station, time) stop_time = stops.find {|s| s.station == station}.time if (stop_time.hour == time.hour) if (stop_time.min > time.min) return true else return false end elsif (stop_time.hour > time.hour) return true else return false end end |
#my_travel_time ⇒ Object
42 43 44 45 46 47 |
# File 'lib/metra/train.rb', line 42 def my_travel_time return nil unless @my_departure and @my_arrival minutes = (@my_arrival.to_i - @my_departure.to_i) / 60 return minutes if minutes > 0 return minutes + 1440 if minutes < 0 end |
#print_my_travel_time ⇒ Object
49 50 51 52 53 |
# File 'lib/metra/train.rb', line 49 def print_my_travel_time if my_travel_time "#{print_my_travel_hours} #{print_my_travel_minutes}".strip end end |