如何使用 Ruby 元编程来重构这个通用代码?
我继承了一个项目,其中有很多写得很糟糕的 Rake 任务,我需要对其进行一些清理。由于 Rakefile 非常庞大,而且常常容易出现奇怪的、无意义的依赖关系,因此我通过将所有内容重构为类来简化和隔离一些事情。
具体来说,该模式如下:
namespace :foobar do
desc "Frozz the foobar."
task :frozzify do
unless Rake.application.lookup('_frozzify')
require 'tasks/foobar'
Foobar.new.frozzify
end
Rake.application['_frozzify'].invoke
end
# Above pattern repeats many times.
end
# Several namespaces, each with tasks that follow this pattern.
在 tasks/foobar.rb
中,我有一些看起来像这样的内容:
class Foobar
def frozzify()
# The real work happens here.
end
# ... Other tasks also in the :foobar namespace.
end
对我来说,这很棒,因为它允许我将任务依赖关系彼此分开,并且将它们完全移动到另一个位置,并且我已经能够极大地简化事情并隔离依赖关系。在您实际尝试运行任务之前,Rakefile 不会满足 require
的要求。以前这会导致严重的问题,因为您甚至无法在不崩溃的情况下列出任务。
我的问题是我经常重复这个习语。请注意以下模式:
对于每个命名空间
:xyz_abc
,在文件tasks/[namespace] 的
,类名类似于tasks/...
中都有一个对应的类.rbXyzAbc
。对于特定命名空间中的每个任务,关联的命名空间类中都有一个名称相同的方法。例如,如果命名空间
:foo_bar
有一个任务:apples
,您会期望在中看到
类,它本身位于def apples() ...
>FooBartasks/foo_bar.rb
中。每个任务
:t
定义一个用于执行实际工作的“元任务”_t
(即带有下划线前缀的任务名称)。
我仍然希望能够为我定义的任务指定一个 desc
描述,并且每个任务的描述都不同。当然,我有一小部分任务根本不遵循上述模式,因此我将在 Rakefile 中手动指定这些任务。
我确信这可以以某种方式重构,这样我就不必一遍又一遍地重复相同的习惯用法,但我缺乏经验来了解如何做到这一点。有人可以帮我吗?
I inherited a project with a lot of badly-written Rake tasks that I need to clean up a bit. Because the Rakefiles are enormous and often prone to bizarre nonsensical dependencies, I'm simplifying and isolating things a bit by refactoring everything to classes.
Specifically, that pattern is the following:
namespace :foobar do
desc "Frozz the foobar."
task :frozzify do
unless Rake.application.lookup('_frozzify')
require 'tasks/foobar'
Foobar.new.frozzify
end
Rake.application['_frozzify'].invoke
end
# Above pattern repeats many times.
end
# Several namespaces, each with tasks that follow this pattern.
In tasks/foobar.rb
, I have something that looks like this:
class Foobar
def frozzify()
# The real work happens here.
end
# ... Other tasks also in the :foobar namespace.
end
For me, this is great, because it allows me to separate the task dependencies from each other and to move them to another location entirely, and I've been able to drastically simplify things and isolate the dependencies. The Rakefile doesn't hit a require
until you actually try to run a task. Previously this was causing serious issues because you couldn't even list the tasks without it blowing up.
My problem is that I'm repeating this idiom very frequently. Notice the following patterns:
For every namespace
:xyz_abc
, there is a corresponding class intasks/...
in the filetasks/[namespace].rb
, with a class name that looks likeXyzAbc
.For every task in a particular namespace, there is an identically named method in the associated namespace class. For example, if namespace
:foo_bar
has a task:apples
, you would expect to seedef apples() ...
inside theFooBar
class, which itself is intasks/foo_bar.rb
.Every task
:t
defines a "meta-task"_t
(that is, the task name prefixed with an underscore) which is used to do the actual work.
I still want to be able to specify a desc
-description for the tasks I define, and that will be different for each task. And, of course, I have a small number of tasks that don't follow the above pattern at all, so I'll be specifying those manually in my Rakefile.
I'm sure that this can be refactored in some way so that I don't have to keep repeating the same idiom over and over, but I lack the experience to see how it could be done. Can someone give me an assist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该对你有用。
更新:添加了描述。
(请注意,我尚未对其进行测试,因此其中可能存在小错误。)
Something like this should work for you.
Update: added descriptions.
(Note that I haven't tested it, so there might be small errors in there.)