class RailFeeds::NetworkRail::Schedule::Association

rubocop:disable Metrics/ClassLength A class for holding information about an association between many trains.

Attributes

associated_location_suffix[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
associated_train_uid[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
category[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
date_indicator[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
end_date[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
main_location_suffix[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
main_train_uid[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
start_date[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent
tiploc[RW]

@!attribute [rw] main_train_uid

@return [String] The UID of the main train in the association.

@!attribute [rw] associated_train_uid

@return [String] The UID of the associated train in the association.

@!attribute [rw] category

@return [String] The category of the association:
* JJ - join
* VV - divide
* NP - next

@!attribute [rw] start_date

@return [Date] When the schedule starts.

@!attribute [rw] end_date

@return [Date] When the schedule ends.

@!attribute [rw] date_indicator

@return [String] When the assocation happens:
* S - same day
* N - over next midnight
* P - over previous midnight

@!attribute [rw] days

@return [Array<Boolean>] The days on which the service runs.
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

@!attribute [rw] tiploc

@return [String] The TIPLOC of the location the association occurs.

@!attribute [rw] base_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the base_uid.

@!attribute [rw] associated_location_suffix

@return [String, nil]
Together with the tiploc uniquely identifies the association
on the associated_uid.

@!attribute [rw] stp_indicator

@return [String]
* C - cancellation of permanent schedule
* N - new STP schedule
* O - STP overlay of permanent schedule
* P - permanent

Public Class Methods

from_cif(line) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new association from a CIF file line

# File lib/rail_feeds/network_rail/schedule/association.rb, line 65
def self.from_cif(line)
  unless %w[AAN AAR AAD].include?(line[0..2])
    fail ArgumentError, "Invalid line:\n#{line}"
  end

  new(
    main_train_uid: line[3..8].strip,
    associated_train_uid: line[9..14].strip,
    start_date: Schedule.make_date(line[15..20]),
    end_date: Schedule.make_date(line[21..26], allow_nil: line[2].eql?('D')),
    days: days_from_cif(line[27..33]),
    category: Schedule.nil_or_strip(line[34..35]),
    date_indicator: Schedule.nil_or_strip(line[36]),
    tiploc: line[37..43].strip,
    main_location_suffix: Schedule.nil_or_i(line[44]),
    associated_location_suffix: Schedule.nil_or_i(line[45]),
    stp_indicator: stp_indicator_from_cif(line[79])
  )
end
from_json(line) click to toggle source

rubocop:disable Metrics/AbcSize Initialize a new association from a JSON file line

# File lib/rail_feeds/network_rail/schedule/association.rb, line 89
def self.from_json(line)
  data = ::JSON.parse(line)['JsonAssociationV1']

  new(
    main_train_uid: data['main_train_uid'],
    associated_train_uid: data['assoc_train_uid'],
    start_date: Date.parse(data['assoc_start_date']),
    end_date: data['assoc_end_date'] ? Date.parse(data['assoc_end_date']) : nil,
    days: days_from_cif(data['assoc_days']),
    category: Schedule.nil_or_strip(data['category']),
    date_indicator: Schedule.nil_or_strip(data['date_indicator']),
    tiploc: data['location'],
    main_location_suffix: Schedule.nil_or_i(data['base_location_suffix']),
    associated_location_suffix: Schedule.nil_or_i(data['assoc_location_suffix']),
    stp_indicator: stp_indicator_from_cif(data['CIF_stp_indicator'])
  )
end
new(**attributes) click to toggle source

Attributes from modules :days, :stp_indicator

# File lib/rail_feeds/network_rail/schedule/association.rb, line 56
def initialize(**attributes)
  attributes.each do |attribute, value|
    send "#{attribute}=", value
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/rail_feeds/network_rail/schedule/association.rb, line 153
def <=>(other)
  start_date <=> other&.start_date
end
==(other) click to toggle source
# File lib/rail_feeds/network_rail/schedule/association.rb, line 148
def ==(other)
  main_train_event_id == other&.main_train_event_id &&
    associated_train_event_id == other&.associated_train_event_id
end
associated_train_event_id() click to toggle source

Uniquely identifies the event on the associated_train_uid

# File lib/rail_feeds/network_rail/schedule/association.rb, line 144
def associated_train_event_id
  "#{tiploc}-#{associated_location_suffix}"
end
divide?() click to toggle source

Test if this is a divide association.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 114
def divide?
  category.eql?('VV')
end
hash() click to toggle source
# File lib/rail_feeds/network_rail/schedule/association.rb, line 157
def hash
  "#{tiploc}-#{main_location_suffix}-#{associated_location_suffix}"
end
join?() click to toggle source

Test if this is a join association.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 109
def join?
  category.eql?('JJ')
end
main_train_event_id() click to toggle source

Uniquely identifies the event on the main_train_uid

# File lib/rail_feeds/network_rail/schedule/association.rb, line 139
def main_train_event_id
  "#{tiploc}-#{main_location_suffix}"
end
next?() click to toggle source

Test if this is a next association.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 119
def next?
  category.eql?('NP')
end
over_next_midnight?() click to toggle source

Test if the association happens over the next midnight.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 129
def over_next_midnight?
  date_indicator.eql?('N')
end
over_previous_midnight?() click to toggle source

Test if the association happens over the previous midnight.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 134
def over_previous_midnight?
  date_indicator.eql?('P')
end
same_day?() click to toggle source

Test if the association happens on the same day.

# File lib/rail_feeds/network_rail/schedule/association.rb, line 124
def same_day?
  date_indicator.eql?('S')
end
to_cif() click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Style/FormatStringToken

# File lib/rail_feeds/network_rail/schedule/association.rb, line 163
def to_cif
  format('%-80.80s', [
    'AAN',
    format('%-6.6s', main_train_uid),
    format('%-6.6s', associated_train_uid),
    format('%-6.6s', start_date&.strftime('%y%m%d')),
    format('%-6.6s', end_date&.strftime('%y%m%d')),
    days_to_cif,
    format('%-2.2s', category),
    format('%-1.1s', date_indicator),
    format('%-7.7s', tiploc),
    format('%-1.1s', main_location_suffix),
    format('%-1.1s', associated_location_suffix),
    'T                                ',
    stp_indicator_to_cif
  ].join) + "\n"
end
to_json(**opts) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/rail_feeds/network_rail/schedule/association.rb, line 184
def to_json(**opts)
  {
    'JsonAssociationV1' => {
      'transaction_type' => 'Create',
      'main_train_uid' => main_train_uid,
      'assoc_train_uid' => associated_train_uid,
      'assoc_start_date' => start_date.strftime('%Y-%m-%dT00:00:00Z'),
      'assoc_end_date' => end_date.strftime('%Y-%m-%dT00:00:00Z'),
      'assoc_days' => days_to_cif,
      'category' => category,
      'date_indicator' => date_indicator,
      'location' => tiploc,
      'base_location_suffix' => main_location_suffix,
      'assoc_location_suffix' => associated_location_suffix,
      'diagram_type' => 'T',
      'CIF_stp_indicator' => stp_indicator_to_cif
    }
  }.to_json(**opts)
end