Sha256: 64c6122e9af58802d7d0b86f544fdee6b1435e4533b29707b5074dad24ff01d8

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Geokit
  module Cached
    module Model

      def self.included(base)
        base.class_eval do
          validates_presence_of :address
        end
      end

      def cache!(attributes)
        self.attributes = attributes
        save if new_record? || changed?
      end

      def update!
        if !by_google? && geo.success
          self.lat, self.lng, self.provider = geo.lat, geo.lng, geo.provider
          save if changed?
        end
      end

      def update_and_return!
        update!
        geoloc
      end

      def geo
        @geo ||= Geokit::Geocoders::MultiGeocoder.geocode(address)
      end

      def fake_geoloc
        geoloc = Geokit::GeoLoc.new
        geoloc.lat, geoloc.lng, geoloc.provider, geoloc.success = lat, lng, provider, success?
        geoloc
      end

      def successful_geoloc
        geo if geocoding_occured? && geo.success
      end

      def geoloc
        successful_geoloc || fake_geoloc
      end

      def by_google?
        provider == 'google'
      end

      def changed_to_google?
        by_google? && provider_changed?
      end

      def changed?
        lat_changed? || lng_changed? || changed_to_google?
      end

      def geocoding_occured?
        !@geo.nil?
      end

      def success?
        !!(lat and lng)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Pr0d1r2-geokit-1.3.2.6 lib/geokit/cached/model.rb