Sha256: ca38f2a3c4eb157005b83b7ce7ad146338676f05708bceb01c8e8e0b465bad6c

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require "geocoder/models/mongoid" 

namespace :geocode do
  desc "Geocode all objects without coordinates."
  task :all => :environment do
    class_name = ENV['CLASS'] || ENV['class']
    sleep_timer = ENV['SLEEP'] || ENV['sleep']
    batch = ENV['BATCH'] || ENV['batch']
    reverse = ENV['REVERSE'] || ENV['reverse']
    raise "Please specify a CLASS (model)" unless class_name
    klass = class_from_string(class_name)
    batch = (batch.to_i unless batch.nil?) || 1000
    orm = (klass < Geocoder::Model::Mongoid) ? 'mongoid' : 'active_record'
    reverse = false unless reverse.to_s.downcase == 'true'

    geocode_record = lambda { |obj|
      reverse ? obj.reverse_geocode : obj.geocode
      obj.save
      sleep(sleep_timer.to_f) unless sleep_timer.nil?
    }

    scope = reverse ? klass.not_reverse_geocoded : klass.not_geocoded
    if orm == 'mongoid'
      scope.each do |obj|
        geocode_record.call(obj)
      end
    elsif orm == 'active_record'
      scope.find_each(batch_size: batch) do |obj|
        geocode_record.call(obj)
      end
    end

  end
end
##
# Get a class object from the string given in the shell environment.
# Similar to ActiveSupport's +constantize+ method.
#
def class_from_string(class_name)
  parts = class_name.split("::")
  constant = Object
  parts.each do |part|
    constant = constant.const_get(part)
  end
  constant
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
geocoder-1.4.4 lib/tasks/geocoder.rake
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/geocoder-1.4.3/lib/tasks/geocoder.rake
geocoder-1.4.3 lib/tasks/geocoder.rake
geocoder-1.4.2 lib/tasks/geocoder.rake