class JpJisCode::Code

Attributes

city_name[RW]
city_name_h[RW]
code[RW]
prefecture_name[RW]
prefecture_name_h[RW]

Public Class Methods

build(ret) click to toggle source
# File lib/jp_jis_code/code.rb, line 7
def self.build(ret)
  city = self.new
  city.code              = ret[:code]
  city.prefecture_name   = ret[:prefecture_name]
  city.city_name         = ret[:city_name]
  city.prefecture_name_h = ret[:prefecture_name_h]
  city.city_name_h       = ret[:city_name_h]
  city
end
check_digit(code) click to toggle source
# File lib/jp_jis_code/code.rb, line 34
def self.check_digit(code)
  digit = code.to_s.chars.map(&:to_i)
  last_digit = (((digit[0] * 6 + digit[1] * 5 + digit[2] * 4 + digit[3] * 3 + digit[4] * 2) % 11)  - 11).abs % 10
  code.to_s + last_digit.to_s
end
find(code) click to toggle source
# File lib/jp_jis_code/code.rb, line 17
def self.find(code)
  n_code = normalized_code(code)
  return unless n_code
  ret = Mapping.data[n_code]
  return unless ret

  build(ret)
end
normalized_code(code) click to toggle source
# File lib/jp_jis_code/code.rb, line 26
def self.normalized_code(code)
  return false if code.nil?
  return false unless code.is_a?(String)
  return check_digit(code) if code.to_s.length == 5
  return code if code.to_s.length == 6
  false
end