-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.rb
48 lines (38 loc) · 929 Bytes
/
route.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require_relative './module/instance_counter'
require_relative './module/validation'
class Route
include InstanceCounter
include Validation
attr_reader :stations
validate :stations, :presence
validate :stations, :route
def initialize(start, finish, stations = nil)
@stations = [start, finish]
validate!
stations&.each { |station| add_station(station) }
register_instance
end
def add_station(station)
@stations.insert(-2, station)
end
def delete_station(station)
@stations.delete(station) unless [start, finish].include? station
end
def print
@stations.each_with_index do |station, index|
unless station == start || station == finish
puts "#{index}. #{station.name}"
end
end
end
def start
@stations.first
end
def finish
@stations.last
end
def name
"#{start.name} - #{finish.name}"
end
end