Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Use hashes not arrays to hold schedule data
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Gauld committed Jun 25, 2018
1 parent 456ad22 commit 28a41f1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 66 deletions.
4 changes: 4 additions & 0 deletions lib/rail_feeds/network_rail/schedule/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def <=>(other)
start_date <=> other&.start_date
end

def hash
"#{tiploc}-#{main_location_suffix}-#{associated_location_suffix}"
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def to_cif
Expand Down
67 changes: 21 additions & 46 deletions lib/rail_feeds/network_rail/schedule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Data
# @!attribute [r] last_header The last header added.
# @return [RailFeeds::NetworkRail::Schedule::Header::CIF]
# @!attribute [r] associations
# @return [Array<RailFeeds::NetworkRail::Schedule::Association>]
# @return [Hash<RailFeeds::NetworkRail::Schedule::Association>]
# @!attribute [r] tiplocs
# @return [Array<RailFeeds::NetworkRail::Schedule::Tiploc>]
# @return [Hash<RailFeeds::NetworkRail::Schedule::Tiploc>]
# @!attribute [r] trains
# @return [Array<RailFeeds::NetworkRail::Schedule::Train>]
# @return [Hash<RailFeeds::NetworkRail::Schedule::Train>]

attr_accessor :last_header, :associations, :tiplocs, :trains

Expand Down Expand Up @@ -73,9 +73,9 @@ def generate_cif
yield "/!! Start of file\n"
yield "/!! Generated: #{header.extracted_at.utc&.strftime('%d/%m/%Y %H:%M')}\n"
yield header.to_cif
tiplocs.sort.each { |tiploc| yield tiploc.to_cif }
associations.sort.each { |association| yield association.to_cif }
trains.sort.each { |train| train.to_cif.each_line { |line| yield line } }
tiplocs.values.sort.each { |tiploc| yield tiploc.to_cif }
associations.values.sort.each { |association| yield association.to_cif }
trains.values.sort.each { |train| train.to_cif.each_line { |line| yield line } }
yield "ZZ#{' ' * 78}\n"
yield "/!! End of file\n"
end
Expand Down Expand Up @@ -104,20 +104,13 @@ def fetch_data(credentials: Credentials)
end
end

# Inplace sort the various data arrays
def sort!
associations.sort!
tiplocs.sort!
trains.sort!
end

private

def reset_data
@last_header = nil
@associations = []
@tiplocs = []
@trains = []
@associations = {}
@tiplocs = {}
@trains = {}
end

# rubocop:disable Metrics/AbcSize
Expand Down Expand Up @@ -161,65 +154,47 @@ def do_header(_parser, header)

# TIPLOC Insert record
def do_tiploc_insert(_parser, tiploc)
tiplocs.push tiploc
tiplocs[tiploc.hash] = tiploc
end

# TIPLOC Amend record
def do_tiploc_amend(parser, tiploc_id, tiploc)
index = tiplocs.index do |t|
t.tiploc.eql?(tiploc_id)
end

if index.nil?
do_tiploc_insert parser, tiploc
else
tiplocs[index] = tiploc
end
def do_tiploc_amend(_parser, tiploc_id, tiploc)
tiplocs[tiploc_id] = tiploc
end

# TIPLOC Delete record
def do_tiploc_delete(_parser, tiploc)
tiplocs.delete tiploc
tiplocs.delete tiploc.hash
end

# Association New record
def do_association_new(_parser, association)
associations.push association
associations[association.hash] = association
end

# Association Revise record
def do_association_revise(parser, association)
index = associations.index(association)
if index.nil?
do_association_new parser, association
else
associations[index] = association
end
def do_association_revise(_parser, association)
associations[association.hash] = association
end

# Association Delete record
def do_association_delete(_parser, association)
associations.delete association
associations.delete association.hash
end

# New Train received
def do_train_new(_parser, train)
trains.push train
trains[train.hash] = train
end

# Revise Train received
def do_train_revise(parser, train)
index = trains.index(train)
if index.nil?
do_train_new parser, train
else
trains[index] = train
end
def do_train_revise(_parser, train)
trains[train.hash] = train
end

# Delete Train record
def do_train_delete(_parser, train)
trains.delete train
trains.delete train.hash
end

# Trailer record
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/network_rail/schedule/starting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
transaction_type: N

:tiplocs:
'1': !ruby/object:RailFeeds::NetworkRail::Schedule::Tiploc
- !ruby/object:RailFeeds::NetworkRail::Schedule::Tiploc
tiploc: '1'
nlc: 1
nlc_check_char: a
nlc_description: Nlc 1
tps_description: Tps 1
stanox: 1
crs: 1
'2': !ruby/object:RailFeeds::NetworkRail::Schedule::Tiploc
- !ruby/object:RailFeeds::NetworkRail::Schedule::Tiploc
tiploc: '2'
nlc: 2
nlc_check_char: b
Expand Down
9 changes: 9 additions & 0 deletions spec/rail_feeds/network_rail/schedule/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@
expect(subject.to_cif).to eq "#{line}\n"
end

describe '#hash' do
it 'Uses tiploc, main_location_suffix and associated_location_suffix' do
subject.tiploc = 'TIPLOC'
subject.main_location_suffix = 1
subject.associated_location_suffix = 2
expect(subject.hash).to eq 'TIPLOC-1-2'
end
end

describe '#<=>' do
let(:association1) { described_class.new start_date: Date.new(2000, 1, 1) }
let(:association2) { described_class.new start_date: Date.new(2000, 1, 2) }
Expand Down
29 changes: 11 additions & 18 deletions spec/rail_feeds/network_rail/schedule/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def parse_cif_file(file)
data.tiplocs.clear
data.trains.clear
data.instance_exec(starting_data[:last_header]) { |h| @last_header = h }
starting_data[:associations].each { |i| data.associations.push i }
starting_data[:trains].each { |i| data.trains.push i }
starting_data[:tiplocs].each { |i| data.tiplocs.push i }
starting_data[:associations].each { |i| data.associations[i.hash] = i }
starting_data[:trains].each { |i| data.trains[i.hash] = i }
starting_data[:tiplocs].each { |i| data.tiplocs[i.hash] = i }
end
data
end
Expand All @@ -53,16 +53,16 @@ def parse_cif_file(file)
end

it 'Tiplocs' do
expect(subject.tiplocs.map(&:tiploc)).to eq ['1', '2', '3']
expect(subject.tiplocs.values.map(&:tiploc)).to eq ['1', '2', '3']
end

it 'Associations' do
expect(subject.associations.map(&:main_location_suffix)).to eq ['a', 'b', 'c']
expect(subject.associations.map(&:category)).to eq ['JJ', 'JJ', 'JJ']
expect(subject.associations.values.map(&:main_location_suffix)).to eq ['a', 'b', 'c']
expect(subject.associations.values.map(&:category)).to eq ['JJ', 'JJ', 'JJ']
end

it 'Trains' do
expect(subject.trains.map(&:signalling_headcode)).to eq [
expect(subject.trains.values.map(&:signalling_headcode)).to eq [
'1A11',
'2B22',
'3C33'
Expand All @@ -83,16 +83,16 @@ def parse_cif_file(file)
end

it 'Tiplocs' do
expect(subject.tiplocs.map(&:tiploc)).to eq ['1', '3a', '4', '5a']
expect(subject.tiplocs.values.map(&:tiploc)).to eq ['1', '3a', '4', '5a']
end

it 'Associations' do
expect(subject.associations.map(&:main_location_suffix)).to eq ['a', 'c', 'd', 'e']
expect(subject.associations.map(&:category)).to eq ['JJ', 'VV', 'JJ', 'JJ']
expect(subject.associations.values.map(&:main_location_suffix)).to eq ['a', 'c', 'd', 'e']
expect(subject.associations.values.map(&:category)).to eq ['JJ', 'VV', 'JJ', 'JJ']
end

it 'Trains' do
expect(subject.trains.map(&:signalling_headcode)).to eq [
expect(subject.trains.values.map(&:signalling_headcode)).to eq [
'1A11',
'3c33',
'4D44',
Expand Down Expand Up @@ -211,11 +211,4 @@ def parse_cif_file(file)
subject.fetch_data
end
end

it '#sort!' do
expect(subject.associations).to receive(:sort!)
expect(subject.tiplocs).to receive(:sort!)
expect(subject.trains).to receive(:sort!)
expect { subject.sort! }.to_not raise_error
end
end

0 comments on commit 28a41f1

Please sign in to comment.