module Geocoder::Store::ActiveRecord

def self.included(base)


Implementation of 'included' hook method.
#
def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    # scope: geocoded objects
    scope :geocoded, lambda {
      {:conditions => "#{geocoder_options[:latitude]} IS NOT NULL " +
        "AND #{geocoder_options[:longitude]} IS NOT NULL"}}
    # scope: not-geocoded objects
    scope :not_geocoded, lambda {
      {:conditions => "#{geocoder_options[:latitude]} IS NULL " +
        "OR #{geocoder_options[:longitude]} IS NULL"}}
    ##
    # Find all objects within a radius of the given location.
    # Location may be either a string to geocode or an array of
    # coordinates (<tt>[lat,lon]</tt>). Also takes an options hash
    # (see Geocoder::Orm::ActiveRecord::ClassMethods.near_scope_options
    # for details).
    #
    scope :near, lambda{ |location, *args|
      latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
      if latitude and longitude
        near_scope_options(latitude, longitude, *args)
      else
        where(:id => false) # no results if no lat/lon given
      end
    }
    ##
    # Find all objects within the area of a given bounding box.
    # Bounds must be an array of locations specifying the southwest
    # corner followed by the northeast corner of the box
    # (<tt>[[sw_lat, sw_lon], [ne_lat, ne_lon]]</tt>).
    #
    scope :within_bounding_box, lambda{ |bounds|
      sw_lat, sw_lng, ne_lat, ne_lng = bounds.flatten if bounds
      return where(:id => false) unless sw_lat && sw_lng && ne_lat && ne_lng
      spans = "latitude BETWEEN #{sw_lat} AND #{ne_lat} AND "
      spans << if sw_lng > ne_lng   # Handle a box that spans 180
        "longitude BETWEEN #{sw_lng} AND 180 OR longitude BETWEEN -180 AND #{ne_lng}"
      else
        "longitude BETWEEN #{sw_lng} AND #{ne_lng}"
      end
      { :conditions => spans }
    }
  end
end

def geocode


(or other as specified in +geocoded_by+). Returns coordinates (array).
Look up coordinates and assign to +latitude+ and +longitude+ attributes
#
def geocode
  do_lookup(false) do |o,rs|
    if r = rs.first
      unless r.latitude.nil? or r.longitude.nil?
        o.send :write_attribute, self.class.geocoder_options[:latitude],  r.latitude
        o.send :write_attribute, self.class.geocoder_options[:longitude], r.longitude
      end
      r.coordinates
    end
  end
end

def reverse_geocode


in +reverse_geocoded_by+). Returns address (string).
Look up address and assign to +address+ attribute (or other as specified
#
def reverse_geocode
  do_lookup(true) do |o,rs|
    r = rs.first
    unless r.address.nil?
      o.send :write_attribute, self.class.geocoder_options[:fetched_address], r.address
    end
    r.address
  end
end