【问题标题】:Access variable from another rake namespace从另一个 rake 命名空间访问变量
【发布时间】:2015-07-23 13:13:26
【问题描述】:

我在嵌套命名空间中有很多 rake 任务。

我的所有任务都具有几乎相同的结构,因此我不想为每个任务编写文档,而是希望使用每个命名空间中存在的配置变量来生成文档。

小例子:

namespace :metadata do |ns|
    config = {:data_location=>"/mnt/metadata/",
              :data_description=>"Metadata from system 5"}
    task :process do |task|
        # Code
    end
end

namespace :frontend_log do |ns|
    config = {:data_location=>"/mnt/backend/",
              :data_description=>"Logdata from backend"}
    task :process do |task|
        # Code
    end
end


namespace :backend_log do |ns|
    config = {:data_location=>"/mnt/backendlog/",
              :data_description=>"Logdata from backend"}
    task :process do |task|
        # Code
    end
end

想象一下还有 150 个这样的命名空间。

namespace :documentation do |ns|
    task :generate do |task|
        # For each namespace that has a configuration
        # Generate a sentence about that namespace
    end
end

输出示例:

:metadata:process 将处理 /mnt/metadata 文件夹中的数据,其中包含来自系统 5 的元数据

:frontend_log:process 将处理来自 /mnt/backend 文件夹的数据,该文件夹包含来自后端的 Logdata

等等。

如何在:documentation:generate 中获取:metadata 的配置?

我这样做是为了避免重构代码,欢迎提出建设性的重构建议,但实际上,这不是我提出这个问题的原因。 p>

【问题讨论】:

  • @jkeuhlen 你的意思是在:metadata 命名空间内创建一个记录它的任务?我目前正在尝试这个,这可能是一个不错的选择。
  • 啊,感谢您的更新,使它更加清晰。我将删除我的答案并重试。

标签: ruby rake


【解决方案1】:

所以到现在我已经用这种方式解决了:

namespace :metadata do |ns|
    config = {:data_location=>"/mnt/metadata/",
              :data_description=>"Metadata from system 5"}
    task :process do |task|
        # Code
    end
    task :document do |task|
        selfdocument(config)
    end
end

namespace :frontend_log do |ns|
    config = {:data_location=>"/mnt/backend/",
              :data_description=>"Logdata from backend"}
    task :process do |task|
        # Code
    end
    task :document do |task|
        selfdocument(config)
    end
end

namespace :backend_log do |ns|
    config = {:data_location=>"/mnt/backendlog/",
              :data_description=>"Logdata from backend"}
    task :process do |task|
        # Code
    end
    task :document do |task|
        selfdocument(task, config)
    end
end

带有辅助函数

def selfdocument(task, config) do
    puts "#{task} will process data from the #{config[:data_location]} folder, which contains #{config[:data_description]}"
end

这不是重写或重构,因为我只是添加一个任务,而不是移动代码或更改现有代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多