Sha256: 30852f6c79f6b38b1b67d49e1b5e97b9eb1a321d90ee16417bc7c1c5485520e7

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 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

4 entries across 4 versions & 1 rubygems

Version Path
japan_etc-0.4.0 lib/japan_etc/road.rb
japan_etc-0.3.0 lib/japan_etc/road.rb
japan_etc-0.2.0 lib/japan_etc/road.rb
japan_etc-0.1.0 lib/japan_etc/road.rb