Sha256: fbb781ae34ce04bab073f6b4b4b2e8b521aa03eba906dc7a5d8871a667883942

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'japan_etc/error'
require 'japan_etc/util'

module JapanETC
  Road = Struct.new(:name, :route_name) do
    include Util

    IRREGULAR_ABBREVIATIONS = {
      '東名高速道路' => '東名',
      '新東名高速道路' => '新東名',
      '名神高速道路' => '名神',
      '新名神高速道路' => '新名神',
      '首都高速道路' => '首都高',
      '首都圏中央連絡自動車道' => '圏央道',
      '東京湾アクアライン' => 'アクアライン',
      '東京湾アクアライン連絡道' => 'アクア連絡道',
      '名古屋第二環状自動車道' => '名二環'
    }

    def initialize(name, route_name = nil)
      raise ValidationError, '#name cannot be nil' if name.nil?

      super(normalize(name), normalize(route_name))
    end

    def abbreviation
      @abbreviation ||=
        if (irregular_abbreviation = IRREGULAR_ABBREVIATIONS[name])
          irregular_abbreviation
        else
          regular_abbreviation
        end
    end

    def regular_abbreviation
      abbreviation = name.dup

      if abbreviation.start_with?('第')
        abbreviation = abbreviation.sub(/高速道路|自動車道|道路/, '')
      end

      abbreviation = abbreviation
        .sub('高速道路', '高速')
        .sub('自動車道', '道')
        .sub('道路', '道')
        .sub('有料', '')

      abbreviation
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
japan_etc-0.5.1 lib/japan_etc/road.rb
japan_etc-0.5.0 lib/japan_etc/road.rb