Why does Thor feature the no_tasks method?
Asked Answered
R

1

6

Or, to be more specific: Can't I just use private methods instead?

Rogerrogerio answered 28/2, 2013 at 9:8 Comment(0)
N
6

As I understand Thor it features ways to mark methods as no tasks because the concept of a method being a task is different from the concept of a method's visibility. They need to be differentiated because they do different jobs.

Every method (independently of its visibility) of a subclass to the class Thor is considered a task unless it is explicitly marked as no task. By controlling the visibility of a task you control access to that task, e.g. if you make a task private it cannot be called by subclasses or directly by a user. Access control is different from whether something is a task or not. Methods that are not tasks cannot be called directly by Thor and they cannot have options.

Here is an example to illustrate. In the following SuperClass inherits the task baseTask and the method this_is_not_a_task from BaseClass. Note that if the task baseTask is marked as private it will not be inherited by SuperClass but this_is_not_a_task will still be inherited.

require 'thor'

class BaseClass < Thor
  method_options :force => :boolean, :alias => :string

  desc 'baseTask', 'Base task'
  def baseTask
    puts this_is_not_a_task("base")
  end

  no_tasks do
    def this_is_not_a_task(s)
      s.upcase
    end
  end

  # private :baseTask
end

class SuperClass < BaseClass
  desc 'superTask', 'Super task'
  def superTask
    puts this_is_not_a_task("super")
  end
end

SuperClass.start(ARGV)
Neat answered 1/3, 2013 at 6:44 Comment(1)
I really like your distinction of "a method being a task is different from the concept of a method's visibility"Rogerrogerio

© 2022 - 2024 — McMap. All rights reserved.