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

- (ZipCodes) initialize(zip_codes)

Returns a new instance of ZipCodes

Parameters:

  • zip_codes (Array<SwissMatch::ZipCode>)

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



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

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

Instance Method Details

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

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:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/swissmatch/zipcodes.rb', line 88

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

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

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.



145
146
147
# File 'lib/swissmatch/zipcodes.rb', line 145

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

- (SwissMatch::ZipCodes) autocomplete(string)

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)



159
160
161
162
163
164
165
166
167
168
# File 'lib/swissmatch/zipcodes.rb', line 159

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

- (SwissMatch::ZipCodes) by_code(code)

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.



197
198
199
200
# File 'lib/swissmatch/zipcodes.rb', line 197

def by_code(code)
  @by_code ||= @zip_codes.group_by { |c| c.code }
  ZipCodes.new(@by_code[code] || [])
end

- (SwissMatch::ZipCode) by_code_and_add_on(code, add_on)

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.



204
205
206
207
# File 'lib/swissmatch/zipcodes.rb', line 204

def by_code_and_add_on(code, add_on)
  @by_full_code ||= Hash[@zip_codes.map { |c| [c.full_code, c] }]
  @by_full_code[code*100+add_on]
end

- (SwissMatch::ZipCode) by_code_and_name(code, name)

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.



211
212
213
214
215
216
# File 'lib/swissmatch/zipcodes.rb', line 211

def by_code_and_name(code, name)
  @by_code_and_name ||= Hash[@zip_codes.flat_map { |c|
    c.names.map { |name| [[c.code, name], c] }
  }]
  @by_code_and_name[[code,name]]
end

- (SwissMatch::ZipCodes) by_name(name)

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.



220
221
222
223
224
225
226
227
228
# File 'lib/swissmatch/zipcodes.rb', line 220

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

- (SwissMatch::ZipCode) by_ordering_number(onrp)

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

Returns:



190
191
192
193
# File 'lib/swissmatch/zipcodes.rb', line 190

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

- (self) each {|zip_code| ... }

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



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

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

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

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.



152
153
154
# File 'lib/swissmatch/zipcodes.rb', line 152

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

- (Object) inspect

See Also:

  • Object#inspect


243
244
245
# File 'lib/swissmatch/zipcodes.rb', line 243

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

- (self) replace(other, reset = true)

Replace the contents of this SwissMatch::ZipCodes collection

Parameters:

  • other (Array<ZipCode>, SwissMatch::ZipCodes)

    The data to replace this SwissMatch::ZipCodes collection with

  • reset (Boolean) (defaults to: true)

    Whether caching values should be reset (only set this to false if you know what you do)

Returns:

  • (self)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/swissmatch/zipcodes.rb', line 43

def replace(other, reset=true)
  case other
    when SwissMatch::ZipCodes
      @zip_codes.replace(other.instance_variable_get(:@zip_codes))
    when Array
      @zip_codes.replace(other)
    else
      raise ArgumentError, "Expected an array or a SwissMatch::ZipCodes, but got #{other.class}"
  end
  reset! if reset

  self
end

- (Object) reset!

Reinitialize all caching instance variables



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

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

- (SwissMatch::ZipCodes) residential

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

Returns:



172
173
174
# File 'lib/swissmatch/zipcodes.rb', line 172

def residential
  with_type(10, 20)
end

- (self) reverse_each {|zip_code| ... }

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



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

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

- (Integer) size

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

Returns:

  • (Integer)

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



231
232
233
# File 'lib/swissmatch/zipcodes.rb', line 231

def size
  @zip_codes.size
end

- (Array<SwissMatch::ZipCode>) to_a

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.



237
238
239
# File 'lib/swissmatch/zipcodes.rb', line 237

def to_a
  @zip_codes.dup
end

- (SwissMatch::ZipCodes) with_type(*types)

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).



178
179
180
# File 'lib/swissmatch/zipcodes.rb', line 178

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

- (SwissMatch::ZipCodes) without_type(*types)

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).



184
185
186
# File 'lib/swissmatch/zipcodes.rb', line 184

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