-
Notifications
You must be signed in to change notification settings - Fork 0
/
station.rb
48 lines (37 loc) · 834 Bytes
/
station.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'
require_relative './module/accessors'
class Station
include InstanceCounter
include Validation
extend Accessors
NAME_FORMAT = /^[a-z]{1,15}$/i.freeze
attr_reader :name, :trains
attr_accessor_with_history :name
validate :name, :presence
validate :name, :format, NAME_FORMAT
@@all = []
def initialize(name)
@name = name
@trains = []
@@all << self
register_instance
validate!
end
def self.all
@@all
end
def each_train
@trains.each { |train| yield train }
end
def trains_type(type)
@trains.select { |train| train.type == type }
end
def add_train(train)
@trains.push(train)
end
def remove_train(train)
@trains.delete(train)
end
end