Class: ZipCode::FR

Inherits:
Object
  • Object
show all
Defined in:
lib/zipcode-fr.rb

Overview

TODO: factor index system out rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initializeFR

Returns a new instance of FR.



8
9
10
# File 'lib/zipcode-fr.rb', line 8

def initialize
  @indexes = {}
end

Instance Method Details

#complete(name, str, key = nil) ⇒ Object



180
181
182
183
# File 'lib/zipcode-fr.rb', line 180

def complete(name, str, key = nil)
  key ||= name
  search(name, str).map { |e| e[key] }
end

#index!(name, data, modes = nil, key: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zipcode-fr.rb', line 70

def index!(name, data, modes = nil, key: nil)
  key ||= name
  index = Hash.new { |h, k| h[k] = [] unless h.frozen? }

  modes = [modes] unless modes.is_a?(Enumerable)
  modes.each do |mode|
    data.each(&appender(index, key, mode))
  end

  index.each_value(&:uniq!)
  index.freeze

  @indexes[name] = index
end

#loadObject



12
13
14
15
16
17
# File 'lib/zipcode-fr.rb', line 12

def load
  # TODO: non-optimal, but not overly long either
  index!(:name, reader, %i[word_prefix match])
  index!(:zip, reader, :prefix)
  @loaded = true
end

#memsize_of_index(name) ⇒ Object



157
158
159
160
161
# File 'lib/zipcode-fr.rb', line 157

def memsize_of_index(name)
  require 'objspace'
  ObjectSpace.memsize_of(@indexes[name]) +
    @indexes[name].reduce(0) { |a, (_, v)| a + ObjectSpace.memsize_of(v) }
end

#ready?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/zipcode-fr.rb', line 19

def ready?
  @loaded
end

#search(name, str, case_insensitive: true) ⇒ Object



175
176
177
178
# File 'lib/zipcode-fr.rb', line 175

def search(name, str, case_insensitive: true)
  str = str.upcase if case_insensitive
  read_at(*index(name)[str.hash])
end