Class: CitySearch
- Inherits:
-
Object
- Object
- CitySearch
- Defined in:
- lib/city_search.rb
Instance Method Summary collapse
- #country_exists?(country) ⇒ Boolean
- #db ⇒ Object
-
#initialize(country = :all) ⇒ CitySearch
constructor
A new instance of CitySearch.
- #search(q) ⇒ Object
- #states ⇒ Object
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
20 21 22 |
# File 'lib/city_search.rb', line 20 def country_exists?(country) File.exists?(File.dirname(__FILE__) + "/../data/#{country}.bin") end |
#db ⇒ Object
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 |
#states ⇒ Object
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 |