From 8a5c8df44113cd9d97846b60274b207e0ff44c1e Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Thu, 22 Mar 2012 09:55:45 -0400 Subject: [PATCH] Fix issue #203: loading namespaced classes. --- lib/tasks/geocoder.rake | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/tasks/geocoder.rake b/lib/tasks/geocoder.rake index f235e1b5..33ed4829 100644 --- a/lib/tasks/geocoder.rake +++ b/lib/tasks/geocoder.rake @@ -3,10 +3,23 @@ namespace :geocode do task :all => :environment do class_name = ENV['CLASS'] || ENV['class'] raise "Please specify a CLASS (model)" unless class_name - klass = Object.const_get(class_name) + klass = class_from_string(class_name) klass.not_geocoded.each do |obj| obj.geocode; obj.save 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 -- GitLab