Created
May 3, 2010 19:01
-
-
Save foysavas/388455 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MappedProperty | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def mapped_property(name, geocoder = "Location") | |
class_eval <<-END, __FILE__, __LINE__ | |
property :#{name}, String | |
property :#{name}_lng, Float | |
property :#{name}_lat, Float | |
property :#{name}_country, String | |
property :#{name}_state, String | |
property :#{name}_city, String | |
def #{name}=(val) | |
attribute_set(:#{name}, val) | |
self.#{name}_lng = self.#{name}_lng = self.#{name}_country = self.#{name}_state = self.#{name}_city = nil | |
begin | |
g = #{geocoder}.geocode(val) | |
if g | |
self.#{name}_lng = g.lng | |
self.#{name}_lat = g.lat | |
self.#{name}_country = g.country_code | |
self.#{name}_state = g.state | |
self.#{name}_city = g.city | |
end | |
rescue | |
end | |
end | |
END | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'geokit' | |
Geokit::Geocoders::google = "YOUR_API_CODE" | |
Geokit::Geocoders::provider_order = [:google] | |
GoogleMap = Geokit::Geocoders::GoogleGeocoder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Location | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, :length => 255, :key => true | |
property :geocode, Object | |
def self.geocode(val) | |
return nil if (val.nil? || val.empty?) | |
l = Location.first(:name => val) | |
return l.geocode if l | |
begin | |
g = GoogleMap.geocode(val) | |
rescue | |
end | |
if g | |
Location.create(:name => val,:geocode => g) | |
end | |
g | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment