module Jisx0402

Constants

TREE_INDEX_KEYS
VERSION

Public Class Methods

data() click to toggle source
# File lib/jisx0402.rb, line 58
def data
  @@data ||= open_data('jisx0402').map do |d|
    Code.new(d)
  end
end
data_trees_index() click to toggle source
# File lib/jisx0402.rb, line 64
def data_trees_index
  @@data_trees_index ||= begin
    TREE_INDEX_KEYS.map.with_object({}) do |idx_key, h|
      h[idx_key] = Jisx0402::Tree::Root.new
      data.each { |d| h[idx_key][d.send(idx_key.to_sym)] = d }
    end
  end
end
find_by_code(c) click to toggle source
# File lib/jisx0402.rb, line 14
def find_by_code(c)
  return nil if !c || c.size == 0

  code = c.to_s
  if code.size == 2 && (1..47).cover?(code.to_i)
    code_without_checkdigit = "#{code}000" # 都道府県
  else
    code_without_checkdigit = code[0..4] # Eliminate checkdigit
  end

  jisx0402_without_checkdigit_table[code_without_checkdigit]
end
forward_match_by(by, chunk) click to toggle source
# File lib/jisx0402.rb, line 35
def forward_match_by(by, chunk)
  ary = data_trees_index[by.to_sym][chunk.to_s] || []
  return Jisx0402::DistrictArray.wrap(ary)
end
forward_match_by_code(chunk) click to toggle source
# File lib/jisx0402.rb, line 31
def forward_match_by_code(chunk)
  forward_match_by(:code, chunk)
end
forward_match_by_full(chunk) click to toggle source
# File lib/jisx0402.rb, line 27
def forward_match_by_full(chunk)
  forward_match_by(:full, chunk)
end
jisx0402_to_zipcode_table() click to toggle source
# File lib/jisx0402.rb, line 92
def jisx0402_to_zipcode_table
  @@jisx0402_to_zipcode_table ||= open_data('jisx0402_to_zipcode')
end
jisx0402_without_checkdigit_table() click to toggle source
# File lib/jisx0402.rb, line 86
def jisx0402_without_checkdigit_table
  @jisx0402_table ||= begin
    data.map{ |d| [d.code_without_checkdigit, d] }.to_h
  end
end
match_by_zipcode(zipcode) click to toggle source
# File lib/jisx0402.rb, line 40
def match_by_zipcode(zipcode)
  zipcode_to_jisx0402_table[zipcode]
end
open_data(fname) click to toggle source
# File lib/jisx0402.rb, line 96
def open_data(fname)
  if Gem::Version.new(MessagePack::VERSION) > Gem::Version.new('0.5.11')
    open_msgpack_data("#{fname}.msgpack")
  else
    open_json_data("#{fname}.json")
  end
end
open_json_data(fname) click to toggle source
# File lib/jisx0402.rb, line 104
def open_json_data(fname)
  JSON.parse(
    open(File.expand_path("../data/#{fname}", __FILE__)).read
  )
end
open_msgpack_data(fname) click to toggle source
# File lib/jisx0402.rb, line 110
def open_msgpack_data(fname)
  MessagePack.unpack(
    open(File.expand_path("../data/#{fname}", __FILE__)).read
  )
end
warmup() click to toggle source
# File lib/jisx0402.rb, line 116
def warmup
  match_by_zipcode('0')
  forward_match_by_full('東')
  true
end
zipcode_to_jisx0402_table() click to toggle source
# File lib/jisx0402.rb, line 73
def zipcode_to_jisx0402_table
  @@zipcode_to_jisx0402_table ||= begin
    jisx0402_to_zipcode_table.map.with_object({}) do |(jisx0402, zipcodes), hash|
      elem = forward_match_by_code(jisx0402).first
      zipcodes.map do |zipcode|
        hash[zipcode] = elem
      end

      hash
    end
  end
end