Class: Trip
- Inherits:
-
Object
- Object
- Trip
- Defined in:
- lib/caltrain/trip.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#headsign ⇒ Object
readonly
Returns the value of attribute headsign.
-
#route_id ⇒ Object
readonly
Returns the value of attribute route_id.
-
#service_id ⇒ Object
readonly
Returns the value of attribute service_id.
-
#shape_id ⇒ Object
readonly
Returns the value of attribute shape_id.
-
#trip_id ⇒ Object
readonly
Returns the value of attribute trip_id.
Class Method Summary collapse
- .<<(trip) ⇒ Object
- .data ⇒ Object
- .new_from_data(data) ⇒ Object
- .saturday_only(dir = nil) ⇒ Object
- .today(dir = nil) ⇒ Object
- .trips(dir = nil) ⇒ Object
- .trips_path ⇒ Object
- .weekday(dir = nil) ⇒ Object
- .weekend(dir = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(*data) ⇒ Trip
constructor
A new instance of Trip.
- #stops(dir = @direction) ⇒ Object
- #time_at_location(loc) ⇒ Object
- #time_data ⇒ Object
- #times ⇒ Object
- #train_no ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(*data) ⇒ Trip
Returns a new instance of Trip.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/caltrain/trip.rb', line 49 def initialize(*data) @trip_id = data[0] @route_id = data[1] @service_id = data[2] @headsign = data[3] @direction = data[4] == '0' ? :north : :south @shape_id = data[5] Trip << self end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def direction @direction end |
#headsign ⇒ Object (readonly)
Returns the value of attribute headsign.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def headsign @headsign end |
#route_id ⇒ Object (readonly)
Returns the value of attribute route_id.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def route_id @route_id end |
#service_id ⇒ Object (readonly)
Returns the value of attribute service_id.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def service_id @service_id end |
#shape_id ⇒ Object (readonly)
Returns the value of attribute shape_id.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def shape_id @shape_id end |
#trip_id ⇒ Object (readonly)
Returns the value of attribute trip_id.
2 3 4 |
# File 'lib/caltrain/trip.rb', line 2 def trip_id @trip_id end |
Class Method Details
.<<(trip) ⇒ Object
44 45 46 |
# File 'lib/caltrain/trip.rb', line 44 def <<(trip) @trips << trip unless trips.map(&:trip_id).include?(trip.trip_id) end |
.data ⇒ Object
9 10 11 |
# File 'lib/caltrain/trip.rb', line 9 def data DataParser.parse(trips_path) end |
.new_from_data(data) ⇒ Object
13 14 15 |
# File 'lib/caltrain/trip.rb', line 13 def new_from_data(data) new(*data) end |
.saturday_only(dir = nil) ⇒ Object
30 31 32 |
# File 'lib/caltrain/trip.rb', line 30 def saturday_only(dir=nil) trips(dir).select { |trip| trip.service_id =~ /^ST/ } end |
.today(dir = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/caltrain/trip.rb', line 34 def today(dir=nil) if Schedule.saturday? weekend(dir) + saturday_only(dir) elsif Schedule.weekend? weekend(dir) else weekday(dir) end end |
.trips(dir = nil) ⇒ Object
17 18 19 20 |
# File 'lib/caltrain/trip.rb', line 17 def trips(dir=nil) @trips ||= [] [:north, :south].include?(dir) ? @trips.select { |trip| trip.direction == dir } : @trips end |
.trips_path ⇒ Object
5 6 7 |
# File 'lib/caltrain/trip.rb', line 5 def trips_path "#{Caltrain.base_dir}/data/google_transit/trips.txt" end |
.weekday(dir = nil) ⇒ Object
26 27 28 |
# File 'lib/caltrain/trip.rb', line 26 def weekday(dir=nil) trips(dir).select { |trip| trip.service_id =~ /^WD/ } end |
.weekend(dir = nil) ⇒ Object
22 23 24 |
# File 'lib/caltrain/trip.rb', line 22 def weekend(dir=nil) trips(dir).select { |trip| trip.service_id =~ /^WE/ } end |
Instance Method Details
#stops(dir = @direction) ⇒ Object
82 83 84 85 |
# File 'lib/caltrain/trip.rb', line 82 def stops(dir=@direction) @stops ||= Schedule.stop_order.select { |stop| time_data.map_nth(3).any? { |d| d =~ /^#{Schedule.abbrevs[stop]}/ } } dir == :north ? @stops : @stops.reverse end |
#time_at_location(loc) ⇒ Object
76 77 78 79 80 |
# File 'lib/caltrain/trip.rb', line 76 def time_at_location(loc) return nil if loc == stops.last loc_str = Schedule.abbrevs[loc] time_data.find { |row| row[3] =~ /^#{loc_str}/ }[1] rescue nil end |
#time_data ⇒ Object
68 69 70 |
# File 'lib/caltrain/trip.rb', line 68 def time_data @time_data ||= Schedule.data.select { |row| row[0] == @trip_id } end |
#times ⇒ Object
72 73 74 |
# File 'lib/caltrain/trip.rb', line 72 def times @times ||= time_data.map_nth(1) end |
#train_no ⇒ Object
60 61 62 |
# File 'lib/caltrain/trip.rb', line 60 def train_no @trip_id =~ /^(\d+)_.+$/; $1.to_i end |
#type ⇒ Object
64 65 66 |
# File 'lib/caltrain/trip.rb', line 64 def type @route_id =~ /^ct_(\w+)_\d+/; $1 end |