Class: Jisx0402::Code

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

Constant Summary collapse

ATTRS_INDEX =
%w(
  code
  prefecture
  prefecture_yomi
  district
  district_yomi
  full
  full_yomi
).freeze
GOVERNMENT_ORDINANCE_DESIGNATED_CITY_CODE_LIST =
%(100 130 140 150).freeze

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ Code

Returns a new instance of Code.



134
135
136
# File 'lib/jisx0402.rb', line 134

def initialize(row)
  @row = row
end

Instance Method Details

#codeObject



142
143
144
# File 'lib/jisx0402.rb', line 142

def code
  @row[0]
end

#code_without_checkdigitObject



146
147
148
# File 'lib/jisx0402.rb', line 146

def code_without_checkdigit
  code[0..-2]
end

#cover?(c) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jisx0402.rb', line 193

def cover?(c)
  return false if !c || c.size == 0

  area_code = c.to_s
  if prefecture?
    area_code.start_with?(prefecture_code)
  elsif government_ordinance_designated_city?
    Jisx0402.forward_match_by_full(full).codes.include?(area_code)
  else
    code == area_code
  end
end

#district(hiragana: false) ⇒ Object



177
178
179
# File 'lib/jisx0402.rb', line 177

def district(hiragana: false)
  hiragana ? @row[4] : @row[3]
end

#district_codeObject



150
151
152
# File 'lib/jisx0402.rb', line 150

def district_code
  @district_code ||= code[2..4]
end

#firstObject



185
186
187
# File 'lib/jisx0402.rb', line 185

def first
  self #compatible for Array#first
end

#full(hiragana: false) ⇒ Object



181
182
183
# File 'lib/jisx0402.rb', line 181

def full(hiragana: false)
  hiragana ? @row[6] : @row[5]
end

#government_ordinance_designated_city?Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/jisx0402.rb', line 159

def government_ordinance_designated_city?
  @government_ordinance_designated_city ||= begin
    GOVERNMENT_ORDINANCE_DESIGNATED_CITY_CODE_LIST.include?(district_code)
  end
end

#match?(word, by) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/jisx0402.rb', line 138

def match?(word, by)
  @row.at(ATTRS_INDEX.index(by.to_s)) =~ /#{word}/
end

#prefecture(hiragana: false) ⇒ Object



173
174
175
# File 'lib/jisx0402.rb', line 173

def prefecture(hiragana: false)
  hiragana ? @row[2] : @row[1]
end

#prefecture?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/jisx0402.rb', line 154

def prefecture?
  district_code == '000'.freeze
end

#prefecture_codeObject



169
170
171
# File 'lib/jisx0402.rb', line 169

def prefecture_code
  code[0..1]
end

#ward?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/jisx0402.rb', line 165

def ward?
  @ward ||= (100..199).cover?(district_code.to_i)
end

#zipcodesObject



189
190
191
# File 'lib/jisx0402.rb', line 189

def zipcodes
  Jisx0402.jisx0402_to_zipcode_table[code_without_checkdigit] || []
end