Skip to content

Commit

Permalink
Merge pull request #124 from CaravanTransit/bus-train-distinction
Browse files Browse the repository at this point in the history
Add bus? and train? methods to Line model
  • Loading branch information
SorenSpicknall committed Feb 27, 2019
2 parents dd8b10a + 73ea0a3 commit 94234d1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/models/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,24 @@ def long_name
def short_name
route_long_name != name ? name : nil
end

# GTFS reference says that a "route_type" of 3 indicates a bus line
# https://developers.google.com/transit/gtfs/reference/#routestxt
def bus?
if system_type.nil? # Prefer system_type if present
vehicle_type == "bus"
else
system_type == 3
end
end

# GTFS reference says that a "route_type" of 0 or 1 indicates a train line
# https://developers.google.com/transit/gtfs/reference/#routestxt
def train?
if system_type.nil?
vehicle_type == "tram" || vehicle_type == "metro"
else
system_type == 0 || system_type == 1
end
end
end
50 changes: 50 additions & 0 deletions test/models/line_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,54 @@ class LineTest < ActiveSupport::TestCase
line = Line.new(route_long_name: route_long_name)
assert_nil line.short_name, "Failed to return correct route short name"
end

test "bus? returns true if system type or vehicle type indicates bus" do
line = Line.new(vehicle_type: "bus")
assert line.bus?

line = Line.new(system_type: 3)
assert line.bus?
end

test "bus? returns false if system type is not bus type" do
line = Line.new(system_type: 1, vehicle_type: "bus")
assert_not line.bus?
end

test "bus? returns false if system type is nil and vehicle type is not bus" do
line = Line.new(vehicle_type: "tram")
assert_not line.bus?
end

test "train? returns true if system type is tram or metro" do
line = Line.new(system_type: 0)
assert line.train?

line = Line.new(system_type: 1)
assert line.train?
end

test "train? returns true if vehicle type is tram or metro" do
line = Line.new(vehicle_type: "tram")
assert line.train?

line = Line.new(vehicle_type: "metro")
assert line.train?
end

test "train? returns false if system type is not tram or metro" do
line = Line.new(system_type: 3, vehicle_type: "tram")
assert_not line.train?
end

test "train? returns false if system type is nil and vehicle type is wrong" do
line = Line.new(vehicle_type: "bus")
assert_not line.train?
end

test "line with no vehicle or system type is not bus? or train?" do
line = Line.new
assert_not line.bus?
assert_not line.train?
end
end

0 comments on commit 94234d1

Please sign in to comment.