如何解决 Object.const_get 在 rake 中失败但在控制台中失败的问题?

发布于 2024-12-29 09:16:10 字数 1471 浏览 2 评论 0原文

我有以下几行代码用作工厂来创建对象:

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...

因此可以在我的规格中并通过控制台创建这些处理器。它们也可以在 rake 中创建——但仅限其中的一部分。其中两个失败了:

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?我可以看到 Rake 和控制台是加载相同的环境(在environment.rb中添加了一些puts),所以我怀疑就是这样。我很困惑可能是什么原因造成的或者去哪里寻找。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2025-01-05 09:16:10

您是否尝试过将 Kernel#require 添加到 rake 文件的顶部以加载正确的类?另外,您如何定义 rake 任务?我已经能够执行此操作来提取我的类常量:

task :my_task => :environment do
  ...
end

另外,检查您的文件名。确保名为“AlphaBravoTangoProcessor”的类位于名为“alpha_bravo_tango_processor.rb”的文件中,而不是名为“alphabravotango_processor.rb”的文件中。

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:

task :my_task => :environment do
  ...
end

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文