Class: AddressLibrary
- Inherits:
-
Object
- Object
- AddressLibrary
- Defined in:
- lib/address_matcher/address_library.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_address(address_string) ⇒ Object
- #address_for_coords(lat_long) ⇒ Object
-
#initialize ⇒ AddressLibrary
constructor
A new instance of AddressLibrary.
- #match(address_string) ⇒ Object
- #near(coords) ⇒ Object
Constructor Details
#initialize ⇒ AddressLibrary
Returns a new instance of AddressLibrary.
8 9 10 11 12 13 14 |
# File 'lib/address_matcher/address_library.rb', line 8 def initialize @store = Hash.new do |outer, o_key| outer[o_key] = Hash.new do |inner, i_key| inner[i_key] = AddressGroup.new end end end |
Class Method Details
.build(address_list) ⇒ Object
2 3 4 5 6 |
# File 'lib/address_matcher/address_library.rb', line 2 def self.build(address_list) new.tap do |library| address_list.each { |address| library.add_address(address) } end end |
Instance Method Details
#add_address(address_string) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/address_matcher/address_library.rb', line 16 def add_address(address_string) coords = geocode(address_string) if coords store[latitude_index(coords)][longitude_index(coords)][coords] = address_string end self end |
#address_for_coords(lat_long) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/address_matcher/address_library.rb', line 35 def address_for_coords(lat_long) unless lat_long.to_a.empty? group = store[latitude_index(lat_long)][longitude_index(lat_long)] group.match(lat_long) end end |
#match(address_string) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/address_matcher/address_library.rb', line 28 def match(address_string) coords = geocode(address_string) if coords address_for_coords(coords) end end |
#near(coords) ⇒ Object
24 25 26 |
# File 'lib/address_matcher/address_library.rb', line 24 def near(coords) store[latitude_index(coords)][longitude_index(coords)] end |