Class: SwissMatch::ZipCodes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/swissmatch/zipcodes.rb

Overview

Represents a collection of SwissMatch::ZipCode objects, and provides a query interface.

Instance Method Summary collapse

Methods included from Enumerable

#last

Constructor Details

#initialize(zip_codes) ⇒ ZipCodes

Returns a new instance of ZipCodes

Parameters:

  • zip_codes (Array<SwissMatch::ZipCode>)

    The SwissMatch::ZipCode objects this SwissMatch::ZipCodes should contain



18
19
20
21
# File 'lib/swissmatch/zipcodes.rb', line 18

def initialize(zip_codes)
  @zip_codes          = zip_codes
  reset!
end

Instance Method Details

#[](key, add_on = nil) ⇒ SwissMatch::ZipCode, SwissMatch::ZipCodes

A convenience method to get one or many zip codes by code, code and add-on, code and city or just city. There are various allowed styles to pass those values. All numeric values can be passed either as Integer or String. You can pass the code and add-on as six-digit number, or you can pass the code as four digit number plus either the add-on or name as second parameter. Or you can pass the code alone, or the name alone.

Examples:

All usage styles

zip_codes[805200]           # zip code 8052, add-on 0
zip_codes["805200"]         # zip code 8052, add-on 0
zip_codes[8052, 0]          # zip code 8052, add-on 0
zip_codes["8052", 0]        # zip code 8052, add-on 0
zip_codes[8052, "0"]        # zip code 8052, add-on 0
zip_codes["8052", 0]        # zip code 8052, add-on 0
zip_codes[8052, "Zürich"]   # zip code 8052, add-on 0
zip_codes["8052", "Zürich"] # zip code 8052, add-on 0
zip_codes[8052]             # all zip codes with code 8052
zip_codes["8052"]           # all zip codes with code 8052
zip_codes["Zürich"]         # all zip codes with name "Zürich"

Parameters:

  • key (String, Integer)

    Either the zip code, zip code and add-on

Returns:

See Also:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/swissmatch/zipcodes.rb', line 67

def [](key, add_on=nil)
  case key
    when /\A(\d{4})(\d\d)\z/
      by_code_and_add_on($1.to_i, $2.to_i)
    when 100_000..999_999
      by_code_and_add_on(*key.divmod(100))
    when 0..9999, /\A\d{4}\z/
      case add_on
        when nil
          by_code(key.to_i)
        when 0..99, /\A\d+\z/
          by_code_and_add_on(key.to_i, add_on.to_i)
        when String
          by_code_and_name(key.to_i, add_on)
        else
          raise ArgumentError,
                "Expected a String, an Integer between 0 and 99, or a String containing an integer between 0 and 99, " \
                "but got #{key.class}: #{key.inspect}"
      end
    when String
      by_name(key)
    else
      raise ArgumentError,
            "Expected a String, an Integer between 1000 and 9999, or an " \
            "Integer between 100_000 and 999_999, but got #{key.class}:" \
            "#{key.inspect}"
  end
end

#active(date = Date.today, &block) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with zip codes that are currently active/in use.

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with zip codes that are currently active/in use.



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

def active(date=Date.today, &block)
  select { |zip_code| zip_code.in_use?(date) }
end

#autocomplete(string) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with zip codes having names that match the given string (prefix search on all languages)

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with zip codes having names that match the given string (prefix search on all languages)



168
169
170
171
172
173
174
175
176
177
# File 'lib/swissmatch/zipcodes.rb', line 168

def autocomplete(string)
  return ZipCodes.new([]) if string.empty? # shortcut

  @autocomplete ||= AutoCompletion.map_keys(@zip_codes) { |zip_code|
    zip_code.transliterated_names
  }
  words = SwissMatch.transliterated_words(string)

  ZipCodes.new(@autocomplete.complete(*words))
end

#autocompleted_names(name) ⇒ Array<String>

Returns An array of ZipCode names which match the given string in an autocompletion. Sorted alphabetically (Umlaut-aware)

Returns:

  • (Array<String>)

    An array of ZipCode names which match the given string in an autocompletion. Sorted alphabetically (Umlaut-aware)



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/swissmatch/zipcodes.rb', line 182

def autocompleted_names(name)
  name_dc = Unicode.downcase(name)
  len     = name_dc.length
  base    = autocomplete(name)
  names   = base.flat_map { |zip_code|
    zip_code.reverse_name_transliteration_map.select { |transliterated_name, real_names|
      Unicode.downcase(transliterated_name[0, len]) == name_dc
    }.values.flatten(1)
  }

  names.uniq.sort(&Unicode.method(:strcmp))
end

#by_code(code) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects having the given 4 digit code.

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects having the given 4 digit code.



234
235
236
# File 'lib/swissmatch/zipcodes.rb', line 234

def by_code(code)
  ZipCodes.new(by_code_lookup_table[code] || [])
end

#by_code_and_add_on(code, add_on) ⇒ SwissMatch::ZipCode

Returns The SwissMatch::ZipCode with the given 4 digit code and given 2 digit code add-on.

Returns:

  • (SwissMatch::ZipCode)

    The SwissMatch::ZipCode with the given 4 digit code and given 2 digit code add-on.



240
241
242
# File 'lib/swissmatch/zipcodes.rb', line 240

def by_code_and_add_on(code, add_on)
  by_full_code_lookup_table[code*100+add_on]
end

#by_code_and_name(code, name) ⇒ SwissMatch::ZipCode

Returns The SwissMatch::ZipCode with the given 4 digit code and name in any language.

Returns:

  • (SwissMatch::ZipCode)

    The SwissMatch::ZipCode with the given 4 digit code and name in any language.



246
247
248
# File 'lib/swissmatch/zipcodes.rb', line 246

def by_code_and_name(code, name)
  by_code_and_name_lookup_table[[code, name]]
end

#by_name(name) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects having the given name.

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects having the given name.



252
253
254
255
256
257
258
259
260
# File 'lib/swissmatch/zipcodes.rb', line 252

def by_name(name)
  @by_name ||= @zip_codes.each_with_object({}) { |zip_code, hash|
    (zip_code.names + zip_code.names_short).map(&:to_s).uniq.each do |name|
      hash[name] ||= []
      hash[name] << zip_code
    end
  }
  ZipCodes.new(@by_name[name] || [])
end

#by_ordering_number(onrp) ⇒ SwissMatch::ZipCode

Returns The SwissMatch::ZipCode with the given ordering number (ONRP).

Returns:



227
228
229
230
# File 'lib/swissmatch/zipcodes.rb', line 227

def by_ordering_number(onrp)
  @by_ordering_number ||= Hash[@zip_codes.map { |c| [c.ordering_number, c] }]
  @by_ordering_number[onrp]
end

#each {|zip_code| ... } ⇒ self

Calls the block once for every SwissMatch::ZipCode in this SwissMatch::ZipCodes instance, passing that zip_code as a parameter. The order is the same as the instance was constructed.

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self



104
105
106
107
# File 'lib/swissmatch/zipcodes.rb', line 104

def each(&block)
  @zip_codes.each(&block)
  self
end

#empty?true, false Also known as: blank?

Returns Whether this zip code collection contains any zip codes.

Returns:

  • (true, false)

    Whether this zip code collection contains any zip codes.



274
275
276
# File 'lib/swissmatch/zipcodes.rb', line 274

def empty?
  @zip_codes.size.zero?
end

#inactive(date = Date.today, &block) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with zip codes that are currently inactive/not in use. A zip code is not in use if it has been either retired or is only recorded for future use.

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with zip codes that are currently inactive/not in use. A zip code is not in use if it has been either retired or is only recorded for future use.



161
162
163
# File 'lib/swissmatch/zipcodes.rb', line 161

def inactive(date=Date.today, &block)
  reject { |zip_code| zip_code.in_use?(date) }
end

#inspectObject

See Also:

  • Object#inspect


281
282
283
# File 'lib/swissmatch/zipcodes.rb', line 281

def inspect
  sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size
end

#names_for_select(language = nil) ⇒ Array<String>

Returns An array of ZipCode names suitable for presentation of a select.

Returns:

  • (Array<String>)

    An array of ZipCode names suitable for presentation of a select.



197
198
199
200
201
202
203
204
205
# File 'lib/swissmatch/zipcodes.rb', line 197

def names_for_select(language=nil)
  if language
    names = flat_map { |zip_code| [zip_code.name, zip_code.suggested_name(I18n.language)] }
  else
    names = map(&:name)
  end

  names.uniq.sort(&Unicode.method(:strcmp))
end

#reject(*args, &block) ⇒ SwissMatch::ZipCodes

A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects for which the block returned false (or a falseish value)

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects for which the block returned false (or a falseish value)



132
133
134
# File 'lib/swissmatch/zipcodes.rb', line 132

def reject(*args, &block)
  ZipCodes.new(@zip_codes.reject(*args, &block))
end

#reset!Object

Reinitialize all caching instance variables



25
26
27
28
29
30
31
32
33
34
# File 'lib/swissmatch/zipcodes.rb', line 25

def reset!
  @by_ordering_number = nil
  @by_code            = nil
  @by_full_code       = nil
  @by_code_and_name   = nil
  @by_name            = nil
  @autocomplete       = nil

  self
end

#residentialSwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection with zip codes of type 10 and 20.

Returns:



209
210
211
# File 'lib/swissmatch/zipcodes.rb', line 209

def residential
  with_type(10, 20)
end

#reverse_each {|zip_code| ... } ⇒ self

Calls the block once for every SwissMatch::ZipCode in this SwissMatch::ZipCodes instance, passing that zip_code as a parameter. The order is the reverse of what the instance was constructed.

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self



117
118
119
120
# File 'lib/swissmatch/zipcodes.rb', line 117

def reverse_each(&block)
  @zip_codes.reverse_each(&block)
  self
end

#select(*args, &block) ⇒ SwissMatch::ZipCodes

A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects for which the block returned true (or a trueish value)

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects for which the block returned true (or a trueish value)



125
126
127
# File 'lib/swissmatch/zipcodes.rb', line 125

def select(*args, &block)
  ZipCodes.new(@zip_codes.select(*args, &block))
end

#sizeInteger

Returns The number of SwissMatch::ZipCode objects in this collection.

Returns:

  • (Integer)

    The number of SwissMatch::ZipCode objects in this collection.



263
264
265
# File 'lib/swissmatch/zipcodes.rb', line 263

def size
  @zip_codes.size
end

#sort(*args, &block) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection sorted by the block.

Returns:

See Also:

  • Enumerable#sort


140
141
142
# File 'lib/swissmatch/zipcodes.rb', line 140

def sort(*args, &block)
  ZipCodes.new(@zip_codes.sort(*args, &block))
end

#sort_by(*args, &block) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection sorted by the block.

Returns:

See Also:

  • Enumerable#sort_by


148
149
150
# File 'lib/swissmatch/zipcodes.rb', line 148

def sort_by(*args, &block)
  ZipCodes.new(@zip_codes.sort_by(*args, &block))
end

#to_aArray<SwissMatch::ZipCode>

Returns A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects in this SwissMatch::ZipCodes.

Returns:

  • (Array<SwissMatch::ZipCode>)

    A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects in this SwissMatch::ZipCodes.



269
270
271
# File 'lib/swissmatch/zipcodes.rb', line 269

def to_a
  @zip_codes.dup
end

#with_type(*types) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection consisting only of zip codes having the given type(s).

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection consisting only of zip codes having the given type(s).



215
216
217
# File 'lib/swissmatch/zipcodes.rb', line 215

def with_type(*types)
  select { |zip_code| types.include?(zip_code.type) }
end

#without_type(*types) ⇒ SwissMatch::ZipCodes

Returns A SwissMatch::ZipCodes collection consisting only of zip codes not having the given type(s).

Returns:

  • (SwissMatch::ZipCodes)

    A SwissMatch::ZipCodes collection consisting only of zip codes not having the given type(s).



221
222
223
# File 'lib/swissmatch/zipcodes.rb', line 221

def without_type(*types)
  reject { |zip_code| types.include?(zip_code.type) }
end