ruby - Access variable from another rake namespace -
i have lot of rake tasks in nested namespaces.
all tasks has pretty same structure, instead of writing documentation each, want generate documentation, using configuration variables exist in each namespace.
small example:
namespace :metadata |ns| config = {:data_location=>"/mnt/metadata/", :data_description=>"metadata system 5"} task :process |task| # code end end namespace :frontend_log |ns| config = {:data_location=>"/mnt/backend/", :data_description=>"logdata backend"} task :process |task| # code end end namespace :backend_log |ns| config = {:data_location=>"/mnt/backendlog/", :data_description=>"logdata backend"} task :process |task| # code end end
imagine 150 more of these namespaces.
namespace :documentation |ns| task :generate |task| # each namespace has configuration # generate sentence namespace end end
output example:
:metadata:process process data /mnt/metadata folder, contains metadata system 5
:frontend_log:process process data /mnt/backend folder, contains logdata backend
and on.
how config of :metadata
inside :documentation:generate
?
i doing avoid refactoring code, suggestions refactor welcome if constructive, really, it's not reason ask question.
so have solved in way:
namespace :metadata |ns| config = {:data_location=>"/mnt/metadata/", :data_description=>"metadata system 5"} task :process |task| # code end task :document |task| selfdocument(config) end end namespace :frontend_log |ns| config = {:data_location=>"/mnt/backend/", :data_description=>"logdata backend"} task :process |task| # code end task :document |task| selfdocument(config) end end namespace :backend_log |ns| config = {:data_location=>"/mnt/backendlog/", :data_description=>"logdata backend"} task :process |task| # code end task :document |task| selfdocument(task, config) end end
with helper function
def selfdocument(task, config) puts "#{task} process data #{config[:data_location]} folder, contains #{config[:data_description]}" end
which not rewrite or refactor per say, since adding task, not moving code around or changing existing code.
Comments
Post a Comment