Sha256: 4e53b6d1c28dcca17de1c1a2c9411045338e005d6c085ccd6bfdc5ca79edcd6e

Contents?: true

Size: 1.49 KB

Versions: 45

Compression:

Stored size: 1.49 KB

Contents

module Geocoder::Store
  module MongoBase

    def self.included_by_model(base)
      base.class_eval do

        scope :geocoded, lambda {
          where(geocoder_options[:coordinates].ne => nil)
        }

        scope :not_geocoded, lambda {
          where(geocoder_options[:coordinates] => nil)
        }
      end
    end

    ##
    # Coordinates [lat,lon] of the object.
    # This method always returns coordinates in lat,lon order,
    # even though internally they are stored in the opposite order.
    #
    def to_coordinates
      coords = send(self.class.geocoder_options[:coordinates])
      coords.is_a?(Array) ? coords.reverse : []
    end

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

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

Version data entries

45 entries across 45 versions & 4 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/stores/mongo_base.rb
geocoder-1.8.2 lib/geocoder/stores/mongo_base.rb
geocoder-1.8.1 lib/geocoder/stores/mongo_base.rb
geocoder-1.8.0 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.5 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.4 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.3 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.2 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.1 lib/geocoder/stores/mongo_base.rb
geocoder-1.7.0 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.7 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.6 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.5 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.4 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.3 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.2 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.1 lib/geocoder/stores/mongo_base.rb
geocoder-1.6.0 lib/geocoder/stores/mongo_base.rb
geocoder-1.5.2 lib/geocoder/stores/mongo_base.rb
really-broken-geocoder-2.0.0 lib/geocoder/stores/mongo_base.rb