Skip to content

Proposal: Require Rake to be written as a class using Rake::DSL

Problem

When writing bigger rake tasks, one often need to extract logic into methods to increase readability.

However a method defined inside a rake task will actually pollute the global Object scope.

Proposal

Have a new rubocop to ask developers to write Rake tasks using the Rake::DSL module. This module allows tasks to be written inside a class definition, thus methods can be declared and won't pollute the global Object space.

This article may better explain the appeal, but I'll copy the example here:

class BicycleTasks
  include Rake::DSL

  def initialize
    namespace :bicycle do
      task :assemble do
        bicycle = Bicycle.new

        # Assemble the bicycle:
        attach_wheels(bicycle)
        attach_handlebars(bicycle)
      end
    end
  end

  private

  def attach_wheels(bicycle)
    # ...
  end

  def attach_handlebars(bicycle)
    # ...
  end
end

# Instantiate the class to define the tasks:
BicycleTasks.new

CustomersDot has been using Rake::DSL with a relative success.


Hi @gitlab-org/maintainers/rails-backend what are your thoughts on this? Thanks.

OSZAR »