Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Thor feature the no_tasks method?

Tags:

ruby

thor

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

like image 504
awendt Avatar asked Feb 28 '13 09:02

awendt


1 Answers

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)
like image 99
N.N. Avatar answered Sep 23 '22 14:09

N.N.