0
我有幾行代碼,我使用的工廠來創建對象:如何解決Object.const_get在rake中失敗但不在控制檯中?
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
第一行可能會奇怪,它需要進行重構。我可以用這些線來創建不同的處理器:
class AlphaProcessor < ServiceProcessor
class BetaProcessor < ServiceProcessor
etc...
所以這些處理器可以在我的規格,並通過控制檯來創建。他們也可以在耙子中創建 - 但只有其中的一些。其中兩個是失敗的:
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'",
所以現在的問題是,我應該怎麼去弄清楚爲什麼這兩個(滿分9)會失敗,但只有在耙?我可以看到Rake和控制檯正在加載相同的環境(在environment.rb中有幾個puts
),所以我不確定這是否正確。我難以理解可能會導致這種情況或尋找的地方。
我會嘗試內核的東西,謝謝你的提示。我想知道爲什麼那些正在工作的人不需要...我在拉動env:'task:nightly_tasks => [:check_connection,:environment] do ...' – jcollum 2012-01-27 18:23:03
我更新了你的答案,解決方案:rake似乎被該類所在文件的名稱所困惑,並且破壞了const_get。 – jcollum 2012-01-28 16:12:36