Class: CitySearch

Inherits:
Object
  • Object
show all
Defined in:
lib/city_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(country = :all) ⇒ CitySearch

Returns a new instance of CitySearch.



4
5
6
7
8
# File 'lib/city_search.rb', line 4

def initialize(country = :all)
  raise 'country not found' unless country_exists?(country)

  @country = country
end

Instance Method Details

#country_exists?(country) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/city_search.rb', line 20

def country_exists?(country)
  File.exists?(File.dirname(__FILE__) + "/../data/#{country}.bin")
end

#dbObject



24
25
26
# File 'lib/city_search.rb', line 24

def db
  @all ||= Dawg.load(File.dirname(__FILE__) + "/../data/#{@country}.bin")
end

#search(q) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/city_search.rb', line 10

def search(q)
  results = db.query(q).reject {|r| r.empty? }
  results.map do |r|
    result = r.split(' ')
    code = result.last
    result.pop
    result += states[code.to_i]
  end
end

#statesObject



28
29
30
31
32
# File 'lib/city_search.rb', line 28

def states
  @states ||= Marshal.load(
    File.read(File.dirname(__FILE__) + "/../data/#{@country}_states.bin")
  )
end