Skip to content

Instantly share code, notes, and snippets.

@waynerobinson
Created October 25, 2012 01:33
Show Gist options
  • Save waynerobinson/3949979 to your computer and use it in GitHub Desktop.
Save waynerobinson/3949979 to your computer and use it in GitHub Desktop.
module MTData
class Base < ActiveRecord::Base
# MT_DATA_DATABASE should have a connection string to one of the
# specific databases.
establish_connection MT_DATA_DATABASE
end
class Street < Base
self.table_name = "street"
end
end
class StreetLookup
InvalidCity = Class.new(StandardError)
def initialize(city, options = {})
raise InvalidCity unless @city_database_map[city]
@city = city
@street_model = options[:street_model] || MTData::Street
@city_database_map = options[:city_database_map] || CITY_DATABASE_MAP
@street_field = options[:street_field] || "street"
end
def find_by_street_name(street_name)
@street_model.find_by_sql(
"SELECT * " +
"FROM #{@city_database_map[city]}.#{@street_model.table_name} " +
"WHERE #{@street_field} LIKE '?%'",
street_name
)
end
end
CITY_DATABASE_MAP = {
brisbane: "brisbane",
perth: "perth"
}
StreetLookup.new(:brisbane, city_database_map: CITY_DATABASE_MAP).find_by_street_name("george")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment