Skip to content

Instantly share code, notes, and snippets.

@fstrozzi
Created February 13, 2012 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fstrozzi/1815481 to your computer and use it in GitHub Desktop.
Save fstrozzi/1815481 to your computer and use it in GitHub Desktop.
def thor_task(klass, task_name, &block)
if program.nil?
warn "WARNING: no program is associated with #{class_name.upcase} task, does not make sense to create a thor task."
return nil
end
if klass
wrapper = self
klass.class_eval do
wrapper.options.each_pair do |name, opt|
method_option name, opt
end #each_pair
# Thor's behavior should be respected passing attributes
define_method task_name do |*args|
#it's mandatory that the first and second parameter are respectively wrapper and task
raise ArgumentError, "wrong number of arguments (#{args.size} for #{block.parameters.size-2})" if args.size != block.parameters.size-2
yield wrapper, self, *args
end
end#class_eval
end #klass
end #thor_task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment