Class: JapanETC::Road

Inherits:
Struct
  • Object
show all
Includes:
Comparable, Util
Defined in:
lib/japan_etc/road.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

convert_fullwidth_characters_to_halfwidth, convert_to_integer, normalize, remove_whitespaces

Constructor Details

#initialize(name, route_name = nil) ⇒ Road

Returns a new instance of Road.

Raises:



23
24
25
26
27
# File 'lib/japan_etc/road.rb', line 23

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

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

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



7
8
9
# File 'lib/japan_etc/road.rb', line 7

def name
  @name
end

#route_nameObject

Returns the value of attribute route_name

Returns:

  • (Object)

    the current value of route_name



7
8
9
# File 'lib/japan_etc/road.rb', line 7

def route_name
  @route_name
end

Instance Method Details

#<=>(other) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/japan_etc/road.rb', line 54

def <=>(other)
  [:name, :route_name].each do |attribute|
    result = send(attribute) <=> other.send(attribute)
    return result unless result.zero?
  end

  0
end

#abbreviationObject



29
30
31
32
33
34
35
36
# File 'lib/japan_etc/road.rb', line 29

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

#regular_abbreviationObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/japan_etc/road.rb', line 38

def regular_abbreviation
  abbreviation = name.dup

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

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

  abbreviation
end