Class: JapaneseAddressParser::Models::City

Inherits:
Object
  • Object
show all
Defined in:
lib/japanese_address_parser/models/city.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, prefecture_code:, name:, name_kana:, name_romaji:) ⇒ City

Returns a new instance of City.



12
13
14
15
16
17
18
# File 'lib/japanese_address_parser/models/city.rb', line 12

def initialize(code:, prefecture_code:, name:, name_kana:, name_romaji:)
  @code = code
  @prefecture_code = prefecture_code
  @name = name
  @name_kana = name_kana
  @name_romaji = name_romaji
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



10
11
12
# File 'lib/japanese_address_parser/models/city.rb', line 10

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/japanese_address_parser/models/city.rb', line 10

def name
  @name
end

#name_kanaObject (readonly)

Returns the value of attribute name_kana.



10
11
12
# File 'lib/japanese_address_parser/models/city.rb', line 10

def name_kana
  @name_kana
end

#name_romajiObject (readonly)

Returns the value of attribute name_romaji.



10
11
12
# File 'lib/japanese_address_parser/models/city.rb', line 10

def name_romaji
  @name_romaji
end

#prefecture_codeObject (readonly)

Returns the value of attribute prefecture_code.



10
11
12
# File 'lib/japanese_address_parser/models/city.rb', line 10

def prefecture_code
  @prefecture_code
end

Instance Method Details

#attributesObject



24
25
26
# File 'lib/japanese_address_parser/models/city.rb', line 24

def attributes
  { code: code, formatted_code: formatted_code, prefecture_code: prefecture_code, name: name, name_kana: name_kana, name_romaji: name_romaji }
end

#formatted_codeObject



20
21
22
# File 'lib/japanese_address_parser/models/city.rb', line 20

def formatted_code
  code.nil? ? 'UNKNOWN' : code
end

#prefectureObject



28
29
30
# File 'lib/japanese_address_parser/models/city.rb', line 28

def prefecture
  ::JapaneseAddressParser::Models::Prefecture.all.find { |prefecture| prefecture.code == prefecture_code }
end

#townsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/japanese_address_parser/models/city.rb', line 32

def towns
  filepath = Pathname(__FILE__).dirname.join("../data/#{prefecture_code}-#{formatted_code}.csv")
  ::CSV.table(filepath, converters: nil).map do |town|
    ::JapaneseAddressParser::Models::Town.new(
      name: town[:name],
      name_kana: town[:name_kana],
      name_romaji: town[:name_romaji],
      nickname: town[:nickname],
      latitude: town[:latitude],
      longitude: town[:longitude]
    )
  end
end