Module: Jisx0402

Defined in:
lib/jisx0402.rb,
lib/jisx0402/version.rb

Defined Under Namespace

Classes: Code, DistrictArray, Tree

Constant Summary collapse

TREE_INDEX_KEYS =
%i(code full)
VERSION =
"1.4.0"

Class Method Summary collapse

Class Method Details

.dataObject



58
59
60
61
62
# File 'lib/jisx0402.rb', line 58

def data
  @@data ||= open_data('jisx0402').map do |d|
    Code.new(d)
  end
end

.data_trees_indexObject



64
65
66
67
68
69
70
71
# 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) ⇒ Object



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



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



31
32
33
# File 'lib/jisx0402.rb', line 31

def forward_match_by_code(chunk)
  forward_match_by(:code, chunk)
end

.forward_match_by_full(chunk) ⇒ Object



27
28
29
# File 'lib/jisx0402.rb', line 27

def forward_match_by_full(chunk)
  forward_match_by(:full, chunk)
end

.jisx0402_to_zipcode_tableObject



92
93
94
# File 'lib/jisx0402.rb', line 92

def jisx0402_to_zipcode_table
  @@jisx0402_to_zipcode_table ||= open_data('jisx0402_to_zipcode')
end

.jisx0402_without_checkdigit_tableObject



86
87
88
89
90
# 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) ⇒ Object



40
41
42
# File 'lib/jisx0402.rb', line 40

def match_by_zipcode(zipcode)
  zipcode_to_jisx0402_table[zipcode]
end

.open_data(fname) ⇒ Object



96
97
98
99
100
101
102
# 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) ⇒ Object



104
105
106
107
108
# 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) ⇒ Object



110
111
112
113
114
# File 'lib/jisx0402.rb', line 110

def open_msgpack_data(fname)
  MessagePack.unpack(
    open(File.expand_path("../data/#{fname}", __FILE__)).read
  )
end

.search(word, by: nil) ⇒ Object



10
11
12
# File 'lib/jisx0402.rb', line 10

def search
  data
end

.warmupObject



116
117
118
119
120
# File 'lib/jisx0402.rb', line 116

def warmup
  match_by_zipcode('0')
  forward_match_by_full('')
  true
end

.zipcode_to_jisx0402_tableObject



73
74
75
76
77
78
79
80
81
82
83
84
# 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