class JapanETC::Tollbooth

Constants

Identifier

Attributes

direction[RW]
entrance_or_exit[RW]
identifier[RW]
name[RW]
notes[RW]
priority[RW]
road[RW]
source[RW]

Public Class Methods

create( road_number:, tollbooth_number:, road_name:, route_name: nil, name:, direction: nil, entrance_or_exit: nil, note: nil, source: nil, priority: 0 ) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 17
def self.create(
  road_number:,
  tollbooth_number:,
  road_name:,
  route_name: nil,
  name:,
  direction: nil,
  entrance_or_exit: nil,
  note: nil,
  source: nil,
  priority: 0
)
  identifier = Identifier.new(road_number, tollbooth_number)
  road = Road.new(road_name, route_name)

  new(
    identifier: identifier,
    road: road,
    name: name,
    direction: direction,
    entrance_or_exit: entrance_or_exit,
    note: note,
    source: source,
    priority: priority
  )
end
new( identifier:, road:, name:, direction:, entrance_or_exit:, note:, source:, priority: ) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 44
def initialize(
  identifier:,
  road:,
  name:,
  direction:,
  entrance_or_exit:,
  note:,
  source:,
  priority:
)
  raise ValidationError if identifier.nil? || road.nil? || name.nil?

  @identifier = identifier
  @road = road
  @name = remove_whitespaces(normalize(name))
  @direction = direction
  @entrance_or_exit = entrance_or_exit
  @notes = []
  notes << normalize(note) if note
  @source = source
  @priority = priority

  normalize!
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 84
def <=>(other)
  result = identifier <=> other.identifier
  return result unless result.zero?

  result = priority <=> other.priority
  return -result unless result.zero? # Tollbooth with higher priority comes first

  return -1 if !obsolete? && other.obsolete?
  return 1 if obsolete? && !other.obsolete?

  %i[road name source].each do |attribute|
    result = send(attribute) <=> other.send(attribute)
    return result unless result.zero?
  end

  0
end
==(other) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 74
def ==(other)
  other.is_a?(self.class) && identifier == other.identifier
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 80
def hash
  identifier.hash
end
initialize_copy(original) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 69
def initialize_copy(original)
  @road = original.road.dup
  @name = original.name.dup
end
obsolete?() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 114
def obsolete?
  notes.any? { |note| note.include?('迄') }
end
to_a() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 102
def to_a
  [
    identifier.to_s,
    road.to_a,
    name,
    direction,
    entrance_or_exit,
    notes.empty? ? nil : notes.join(' '),
    source
  ].flatten
end

Private Instance Methods

extract_direction_from_name!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 187
def extract_direction_from_name!
  name.sub!(/(?:上り|下り|[東西南北]行き?)/) do |match|
    found_direction = Direction.from(match)

    if direction
      found_direction == direction ? '' : match
    else
      @direction = found_direction
      ''
    end
  end
end
extract_direction_from_notes!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 157
def extract_direction_from_notes!
  notes.reject! do |note|
    found_direction = Direction.from(note)
    next false unless found_direction

    if direction
      raise ValidationError unless found_direction == direction
    else
      @direction = found_direction
    end

    true
  end
end
extract_entrance_or_exit_from_name!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 200
def extract_entrance_or_exit_from_name!
  name.sub!(/(?:入口|出口|料金所)/) do |match|
    found_entrance_or_exit = EntranceOrExit.from(match)
    found_entrance_or_exit ||= EntranceOrExit::EXIT

    if entrance_or_exit
      found_entrance_or_exit == entrance_or_exit ? '' : match
    else
      @entrance_or_exit = found_entrance_or_exit
      ''
    end
  end
end
extract_entrance_or_exit_from_notes!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 172
def extract_entrance_or_exit_from_notes!
  notes.reject! do |note|
    found_entrance_or_exit = EntranceOrExit.from(note)
    next false unless found_entrance_or_exit

    if entrance_or_exit
      raise ValidationError unless found_entrance_or_exit == entrance_or_exit
    else
      @entrance_or_exit = found_entrance_or_exit
    end

    true
  end
end
extract_name_from_notes!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 214
def extract_name_from_notes!
  name_was_changed = notes.reject! do |note|
    match = note.match(/「(?<new_name>.+?)」へ名称変更/)
    next false unless match

    @name = normalize(match[:new_name])

    true
  end

  name_was_changed
end
extract_notes_from_name!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 130
def extract_notes_from_name!
  name.sub!(/(?<head>.+?)?\s*[(\(](?<note>.+?)[)\)]\s*(?<tail>.+)?/) do
    match = Regexp.last_match

    if match[:head]
      prepend_to_notes(match[:tail]) if match[:tail]
      prepend_to_notes(match[:note])
      match[:head]
    elsif match[:tail]
      prepend_to_notes(match[:note])
      match[:tail]
    else
      match[:note]
    end
  end

  name.sub!(/第[一二三]\z/) do |match|
    prepend_to_notes(match)
    ''
  end

  name.sub!(/合併\z/) do |match|
    prepend_to_notes(match) unless notes.any? { |note| note.include?('合併') }
    ''
  end
end
normalize!() click to toggle source
# File lib/japan_etc/tollbooth.rb, line 120
def normalize!
  extract_notes_from_name!
  extract_direction_from_notes!
  extract_entrance_or_exit_from_notes!
  extract_direction_from_name!
  extract_entrance_or_exit_from_name!
  name_was_changed = extract_name_from_notes!
  normalize! if name_was_changed
end
prepend_to_notes(note) click to toggle source
# File lib/japan_etc/tollbooth.rb, line 227
def prepend_to_notes(note)
  note = normalize(note)
  notes.prepend(note)
end