Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring - moved lazy lookup table generation into private methods.

  • Loading branch information...
commit 2bdab1ca4cfb8272d8dc9ef61ac04bc8e73280d6 1 parent fd6c3db
@awd-switzerland awd-switzerland authored
Showing with 18 additions and 8 deletions.
  1. +18 −8 lib/swissmatch/zipcodes.rb
View
26 lib/swissmatch/zipcodes.rb
@@ -203,24 +203,19 @@ def by_ordering_number(onrp)
# @return [SwissMatch::ZipCodes]
# A SwissMatch::ZipCodes collection with all SwissMatch::ZipCode objects having the given 4 digit code.
def by_code(code)
- @by_code ||= @zip_codes.group_by { |c| c.code }
- ZipCodes.new(@by_code[code] || [])
+ ZipCodes.new(by_code_lookup_table[code] || [])
end
# @return [SwissMatch::ZipCode]
# The SwissMatch::ZipCode with the given 4 digit code and given 2 digit code add-on.
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]
+ by_full_code_lookup_table[code*100+add_on]
end
# @return [SwissMatch::ZipCode]
# The SwissMatch::ZipCode with the given 4 digit code and name in any language.
def by_code_and_name(code, name)
- @by_code_and_name ||= Hash[@zip_codes.flat_map { |c|
- (c.names + c.names_short).map(&:to_s).uniq.map { |name| [[c.code, name], c] }
- }]
- @by_code_and_name[[code,name]]
+ by_code_and_name_lookup_table[[code, name]]
end
# @return [SwissMatch::ZipCodes]
@@ -257,5 +252,20 @@ def empty?
def inspect
sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size
end
+
+ private
+ def by_code_lookup_table
+ @by_code ||= @zip_codes.group_by { |c| c.code }
+ end
+
+ def by_full_code_lookup_table
+ @by_full_code ||= Hash[@zip_codes.map { |c| [c.full_code, c] }]
+ end
+
+ def by_code_and_name_lookup_table
+ @by_code_and_name ||= Hash[@zip_codes.flat_map { |c|
+ (c.names + c.names_short).map(&:to_s).uniq.map { |name| [[c.code, name], c] }
+ }]
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.