Ruby - Thor execute a specific Task first
You can use invoke
to run other tasks:
def show_version
invoke :connect_to_database
# ...
end
That will also make sure that they are run only once, otherwise you can just call the method as usual, e.g.
def show_version
connect_to_database
# ...
end
Or you could add the call to the constructor, to have it run first in every invocation:
def initialize(*args)
super
connecto_to_database
end
The call to super
is very important, without it Thor will have no idea what to do.