I have these lines of code that I use as a Factory to create objects:
class ServiceProcessor
def self.create(service, logger)
classified_name = service.name.to_s.split('_').collect! { |w| w }.join << "Processor"
logger.warn "Creating service proc, classified name: #{classified_name}"
service_proc = Object.const_get(classified_name).new
... check respond_to etc.
return service_proc
The first line might be odd, it needs to be refactored. I can use these lines to create various Processors:
class AlphaProcessor < ServiceProcessor
class BetaProcessor < ServiceProcessor
etc...
So these processors can be created in my specs and via the console. They can be created in rake as well — but only some of them. Two of them are failing:
WARN 2012-01-27 08:54:18 -0800 (25626) Creating service proc,
classified name: AlphaProcessor
ERROR 2012-01-27 08:54:18 -0800 (25626)
Failed for service #<Service _id: 4f203c171d41c83b3b000003, _type: nil,
deleted_at: nil, name: "Alpha", enabled: true>
ERROR 2012-01-27 08:54:18
-0800 (25626) Exception: uninitialized constant AlphaProcessor ERROR
2012-01-27 08:54:18 -0800 (25626) Backtrace:
["/mnt/hgfs/kodiak/lib/service_processors/_service_processor.rb:33:in
`const_get'",
"/mnt/hgfs/kodiak/lib/service_processors/_service_processor.rb:33:in
`create'", "/mnt/hgfs/kodiak/lib/update_engine.rb:28:in `block in
update_all'",
So the question is, how should I go about figuring out why these two (out of 9) would fail, but only in Rake? I can see that Rake and the Console are loading the same environment (a few puts in the environment.rb), so I doubt that’s it. I’m stumped on what could be causing this or where to look.
Have you tried adding a Kernel#require to the top of your rake file to load the proper classes? Also, how are you defining the rake task? I’ve been able to do this for pulling in my class constants:
Also, check your filenames. Make sure that a class named “AlphaBravoTangoProcessor” is in a file named ‘alpha_bravo_tango_processor.rb’, not a file named ‘alphabravotango_processor.rb’.