Class: MetraSchedule::Train

Inherits:
Object
  • Object
show all
Defined in:
lib/metra/train.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  unless options.empty?
    @train_num = options[:train_num] if options.has_key?(:train_num)
    @schedule = options[:schedule] if options.has_key?(:schedule)
    @bike_limit = options[:bike_limit] if options.has_key?(:bike_limit)
    @direction = options[:direction] if options.has_key?(:direction)
    @stops = options[:stops] if options.has_key?(:stops)
  end
end

Instance Attribute Details

#bike_limitObject (readonly)

Returns the value of attribute bike_limit.



3
4
5
# File 'lib/metra/train.rb', line 3

def bike_limit
  @bike_limit
end

#directionObject (readonly)

Returns the value of attribute direction.



3
4
5
# File 'lib/metra/train.rb', line 3

def direction
  @direction
end

#my_arrivalObject

Injected when the line is filtered



4
5
6
# File 'lib/metra/train.rb', line 4

def my_arrival
  @my_arrival
end

#my_departureObject

Injected when the line is filtered



4
5
6
# File 'lib/metra/train.rb', line 4

def my_departure
  @my_departure
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



3
4
5
# File 'lib/metra/train.rb', line 3

def schedule
  @schedule
end

#stopsObject (readonly)

Returns the value of attribute stops.



3
4
5
# File 'lib/metra/train.rb', line 3

def stops
  @stops
end

#train_numObject (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

Returns:

  • (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

Returns:

  • (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_timeObject



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


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