Why does Thor have a no_tasks method?

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

+6
source share
1 answer

As I understand it, Thor shows ways to mark methods as no tasks , because the concept of the method being the task is different from the concept of the visibility method. They must be differentiated because they perform different tasks.

Each method (regardless of its visibility) of a subclass of the Thor class is considered a task if it is not explicitly marked as no task. By controlling the visibility of a task, you control access to this task, for example. if you make a task private, you cannot call it subclasses or directly by the 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 options .

Here is an example to illustrate. In the following, SuperClass inherits the baseTask task and the this_is_not_a_task method from BaseClass . Please note: if the baseTask task baseTask marked as closed, 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) 
+5
source

All Articles