10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/dm-geokit/resource.rb', line 10
def has_geographic_location(name, options = {})
return if self.included_modules.include?(DataMapper::GeoKit::InstanceMethods)
send :include, InstanceMethods
send :include, ::GeoKit::Mappable
if defined?(DataMapper::Validations)
property name.to_sym, String, :length => 255, :auto_validation => false
else
property name.to_sym, String, :length => 255
end
property "#{name}_distance".to_sym, Float
PROPERTY_NAMES.each do |p|
if p.match(/l(at|ng)/)
property "#{name}_#{p}".to_sym, Float, :precision => 15, :scale => 12, :index => true
else
property "#{name}_#{p}".to_sym, String, :length => 255
end
end
if options[:auto_geocode] == true or options[:auto_geocode].nil?
define_method :auto_geocode? do
true
end
else
define_method :auto_geocode? do
false
end
end
define_method "#{name}" do
if attribute_get(name.to_sym).nil?
nil
else
GeographicLocation.new(name, self)
end
end
define_method "#{name}=" do |value|
if value.nil?
nil
elsif value.is_a?(String)
if auto_geocode?
geo = ::GeoKit::Geocoders::MultiGeocoder.geocode(value)
if geo.success?
attribute_set(name.to_sym, geo.full_address)
PROPERTY_NAMES.each do |p|
attribute_set("#{name}_#{p}".to_sym, geo.send(p.to_sym))
end
end
else
attribute_set(name.to_sym, value)
end
end
end
end
|