Sha256: 4a05004fd6874a704abc7f8ffefb57425aaefb02d2b3ecffa01ddf12cbea587b

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

require 'geocoder'

module Geocoder

  ##
  # Methods for invoking Geocoder in a model.
  #
  module Model
    module MongoBase

      ##
      # Set attribute names and include the Geocoder module.
      #
      def geocoded_by(address_attr, options = {}, &block)
        geocoder_init(
          :geocode       => true,
          :user_address  => address_attr,
          :coordinates   => options[:coordinates] || :coordinates,
          :geocode_block => block
        )
      end

      ##
      # Set attribute names and include the Geocoder module.
      #
      def reverse_geocoded_by(coordinates_attr, options = {}, &block)
        geocoder_init(
          :reverse_geocode => true,
          :fetched_address => options[:address] || :address,
          :coordinates     => coordinates_attr,
          :reverse_block   => block
        )
      end

      private # ----------------------------------------------------------------

      def geocoder_init(options)
        unless geocoder_initialized?
          @geocoder_options = {}
          require "geocoder/stores/#{geocoder_file_name}"
          include Geocoder::Store.const_get(geocoder_module_name)
        end
        @geocoder_options.merge! options
      end

      def geocoder_initialized?
        begin
          included_modules.include? Geocoder::Store.const_get(geocoder_module_name)
        rescue NameError
          false
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geocoder-1.1.1 lib/geocoder/models/mongo_base.rb
geocoder-1.1.0 lib/geocoder/models/mongo_base.rb
geocoder-1.0.5 lib/geocoder/models/mongo_base.rb
geocoder-1.0.4 lib/geocoder/models/mongo_base.rb
geocoder-1.0.3 lib/geocoder/models/mongo_base.rb