Class: RailFeeds::NetworkRail::Schedule::Association
- Inherits:
-
Object
- Object
- RailFeeds::NetworkRail::Schedule::Association
- Includes:
- Comparable, Days, STPIndicator
- Defined in:
- lib/rail_feeds/network_rail/schedule/association.rb
Overview
rubocop:disable Metrics/ClassLength A class for holding information about an association between many trains.
Instance Attribute Summary collapse
-
#associated_location_suffix ⇒ String?
Together with the tiploc uniquely identifies the association on the associated_uid.
-
#associated_train_uid ⇒ String
The UID of the associated train in the association.
-
#base_location_suffix ⇒ String?
Together with the tiploc uniquely identifies the association on the base_uid.
-
#category ⇒ String
-
JJ - join * VV - divide * NP - next.
-
-
#date_indicator ⇒ String
-
S - same day * N - over next midnight * P - over previous midnight.
-
-
#days ⇒ Array<Boolean>
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday].
-
#end_date ⇒ Date
When the schedule ends.
-
#main_location_suffix ⇒ Object
Returns the value of attribute main_location_suffix.
-
#main_train_uid ⇒ String
The UID of the main train in the association.
-
#start_date ⇒ Date
When the schedule starts.
-
#stp_indicator ⇒ String
-
C - cancellation of permanent schedule * N - new STP schedule * O - STP overlay of permanent schedule * P - permanent.
-
-
#tiploc ⇒ String
The TIPLOC of the location the association occurs.
Class Method Summary collapse
-
.from_cif(line) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new association from a CIF file line.
-
.from_json(line) ⇒ Object
rubocop:disable Metrics/AbcSize Initialize a new association from a JSON file line.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#associated_train_event_id ⇒ Object
Uniquely identifies the event on the associated_train_uid.
-
#divide? ⇒ Boolean
Test if this is a divide association.
- #hash ⇒ Object
-
#initialize(**attributes) ⇒ Association
constructor
Attributes from modules :days, :stp_indicator.
-
#join? ⇒ Boolean
Test if this is a join association.
-
#main_train_event_id ⇒ Object
Uniquely identifies the event on the main_train_uid.
-
#next? ⇒ Boolean
Test if this is a next association.
-
#over_next_midnight? ⇒ Boolean
Test if the association happens over the next midnight.
-
#over_previous_midnight? ⇒ Boolean
Test if the association happens over the previous midnight.
-
#same_day? ⇒ Boolean
Test if the association happens on the same day.
-
#to_cif ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Style/FormatStringToken.
-
#to_json(**opts) ⇒ Object
rubocop:disable Metrics/MethodLength.
Methods included from STPIndicator
Methods included from Days
#fridays?, included, #mondays?, #saturdays?, #sundays?, #thursdays?, #tuesdays?, #wednesdays?
Constructor Details
#initialize(**attributes) ⇒ Association
Attributes from modules :days, :stp_indicator
56 57 58 59 60 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 56 def initialize(**attributes) attributes.each do |attribute, value| send "#{attribute}=", value end end |
Instance Attribute Details
#associated_location_suffix ⇒ String?
Together with the tiploc uniquely identifies the association on the associated_uid.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#associated_train_uid ⇒ String
Returns The UID of the associated train in the association.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#base_location_suffix ⇒ String?
Together with the tiploc uniquely identifies the association on the base_uid.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#category ⇒ String
-
JJ - join
-
VV - divide
-
NP - next
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#date_indicator ⇒ String
-
S - same day
-
N - over next midnight
-
P - over previous midnight
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#days ⇒ Array<Boolean>
- Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#end_date ⇒ Date
Returns When the schedule ends.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#main_location_suffix ⇒ Object
Returns the value of attribute main_location_suffix.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 def main_location_suffix @main_location_suffix end |
#main_train_uid ⇒ String
Returns The UID of the main train in the association.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 def main_train_uid @main_train_uid end |
#start_date ⇒ Date
Returns When the schedule starts.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#stp_indicator ⇒ String
-
C - cancellation of permanent schedule
-
N - new STP schedule
-
O - STP overlay of permanent schedule
-
P - permanent
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
#tiploc ⇒ String
Returns The TIPLOC of the location the association occurs.
51 52 53 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51 attr_accessor :main_train_uid, :associated_train_uid, :category, :start_date, :end_date, :date_indicator, :tiploc, :main_location_suffix, :associated_location_suffix |
Class Method Details
.from_cif(line) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new association from a CIF file line
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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) ⇒ Object
rubocop:disable Metrics/AbcSize Initialize a new association from a JSON file line
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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 |
Instance Method Details
#<=>(other) ⇒ Object
153 154 155 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 153 def <=>(other) start_date <=> other&.start_date end |
#==(other) ⇒ Object
148 149 150 151 |
# 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 ⇒ Object
Uniquely identifies the event on the associated_train_uid
144 145 146 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 144 def associated_train_event_id "#{tiploc}-#{associated_location_suffix}" end |
#divide? ⇒ Boolean
Test if this is a divide association.
114 115 116 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 114 def divide? category.eql?('VV') end |
#hash ⇒ Object
157 158 159 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 157 def hash "#{tiploc}-#{main_location_suffix}-#{associated_location_suffix}" end |
#join? ⇒ Boolean
Test if this is a join association.
109 110 111 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 109 def join? category.eql?('JJ') end |
#main_train_event_id ⇒ Object
Uniquely identifies the event on the main_train_uid
139 140 141 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 139 def main_train_event_id "#{tiploc}-#{main_location_suffix}" end |
#next? ⇒ Boolean
Test if this is a next association.
119 120 121 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 119 def next? category.eql?('NP') end |
#over_next_midnight? ⇒ Boolean
Test if the association happens over the next midnight.
129 130 131 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 129 def over_next_midnight? date_indicator.eql?('N') end |
#over_previous_midnight? ⇒ Boolean
Test if the association happens over the previous midnight.
134 135 136 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 134 def over_previous_midnight? date_indicator.eql?('P') end |
#same_day? ⇒ Boolean
Test if the association happens on the same day.
124 125 126 |
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 124 def same_day? date_indicator.eql?('S') end |
#to_cif ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Style/FormatStringToken
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# 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) ⇒ Object
rubocop:disable Metrics/MethodLength
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# 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 |