I’m trying to figure out how the Thor gem creates a DSL like this (first example from their README)
class App < Thor # [1]
map "-L" => :list # [2]
desc "install APP_NAME", "install one of the available apps" # [3]
method_options :force => :boolean, :alias => :string # [4]
def install(name)
user_alias = options[:alias]
if options.force?
# do something
end
# other code
end
desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
def list(search="")
# list everything
end
end
Specifically, how does it know which method to map the desc and method_options call to?
descis pretty easy to implement, the trick is to useModule.method_added:any class that inherits from
DescMethodswill have adescmethod likeThor. For each method a message will be printed with the method name and description. For example:when this class is defined the string “test described as Hello world” will be printed.