Class: Trainworks::Railroad

Inherits:
Object
  • Object
show all
Defined in:
lib/trainworks/railroad.rb

Overview

Railroad is the entry point for Trainworks gem

Instance Method Summary collapse

Constructor Details

#initialize(file_path, file_parser: FileParser, graph_builder: GraphBuilder, graph_algorithm: GraphAlgorithm) ⇒ Railroad



8
9
10
11
12
# File 'lib/trainworks/railroad.rb', line 8

def initialize(file_path, file_parser: FileParser, graph_builder: GraphBuilder, graph_algorithm: GraphAlgorithm)
  @file_parser = file_parser.new(file_path)
  @graph = graph_builder.build(@file_parser.parse)
  @algorithm = graph_algorithm.new(@graph)
end

Instance Method Details

#distance(route_string) ⇒ Number

Calculates the distance traveled between cities in the form of "A-B-C"



17
18
19
# File 'lib/trainworks/railroad.rb', line 17

def distance(route_string)
  @algorithm.distance(route_string.split('-'))
end

#shortest_distance(from:, to:) ⇒ Number

Calculates the shortest distance between from and to



49
50
51
# File 'lib/trainworks/railroad.rb', line 49

def shortest_distance(from:, to:)
  @algorithm.shortest_distance(from: from, to: to)
end

#trips(from:, to:, with_max_stops: nil, with_exact_stops: nil, with_max_distance: nil) ⇒ Array<Array>

Notice that only one of the parameters with_max_stops, with_exact_stops or with_max_distance will be taken into account, in the following order:

  1. with_max_stops
  2. with_exact_stops
  3. with_max_distance


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trainworks/railroad.rb', line 33

def trips(from:, to:, with_max_stops: nil, with_exact_stops: nil, with_max_distance: nil)
  if with_max_stops
    @algorithm.trips_with_max_stops(from: from, to: to, stops: with_max_stops)
  elsif with_exact_stops
    @algorithm.trips_with_exact_stops(from: from, to: to, stops: with_exact_stops)
  elsif with_max_distance
    @algorithm.trips_with_max_distance(from: from, to: to, max_distance: with_max_distance)
  else
    raise "I don't know how to calculate these routes"
  end
end