Class: JpJisCode::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/jp_jis_code/code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#city_nameObject

Returns the value of attribute city_name.



5
6
7
# File 'lib/jp_jis_code/code.rb', line 5

def city_name
  @city_name
end

#city_name_hObject

Returns the value of attribute city_name_h.



5
6
7
# File 'lib/jp_jis_code/code.rb', line 5

def city_name_h
  @city_name_h
end

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/jp_jis_code/code.rb', line 5

def code
  @code
end

#prefecture_nameObject

Returns the value of attribute prefecture_name.



5
6
7
# File 'lib/jp_jis_code/code.rb', line 5

def prefecture_name
  @prefecture_name
end

#prefecture_name_hObject

Returns the value of attribute prefecture_name_h.



5
6
7
# File 'lib/jp_jis_code/code.rb', line 5

def prefecture_name_h
  @prefecture_name_h
end

Class Method Details

.build(ret) ⇒ Object



7
8
9
10
11
12
13
14
15
# 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) ⇒ Object



34
35
36
37
38
# 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) ⇒ Object



17
18
19
20
21
22
23
24
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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