Sha256: 9715ea19b003ccb3f6f1fd9b2edaafe112b1f262964777c88d54236ea916ae42

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

require 'thor/error'
require 'thor/util'

class Thor
  class Task < Struct.new(:meth, :description, :usage, :opts, :klass)
    def self.dynamic(meth, klass)
      new(meth, "A dynamically-generated task", meth.to_s, nil, klass)
    end

    def parse(obj, args)
      list, hash = parse_args(args)
      obj.options = hash
      run(obj, *list)
    end

    def run(obj, *params)
      raise NoMethodError, "the `#{meth}' task of #{obj.class} is private" if
        (obj.private_methods + obj.protected_methods).include?(meth)
      
      obj.send(meth, *params)
    rescue ArgumentError => e
      # backtrace sans anything in this file
      backtrace = e.backtrace.reject {|frame| frame =~ /^#{Regexp.escape(__FILE__)}/}
      # and sans anything that got us here
      backtrace -= caller
      raise e unless backtrace.empty?
    
      # okay, they really did call it wrong
      raise Error, "`#{meth}' was called incorrectly. Call as `#{formatted_usage}'"
    rescue NoMethodError => e
      begin
        raise e unless e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(obj.inspect)}$/
      rescue
        raise e
      end
      raise Error, "The #{namespace false} namespace doesn't have a `#{meth}' task"
    end

    def namespace(remove_default = true)
      Thor::Util.constant_to_thor_path(klass, remove_default)
    end

    def with_klass(klass)
      new = self.dup
      new.klass = klass
      new
    end
    
    def opts
      return super unless super.kind_of? Hash
      self.opts = Options.new(super)
    end

    def formatted_usage(namespace = false)
      (namespace ? self.namespace + ':' : '') + usage +
        (opts ? " " + opts.formatted_usage : "")
    end

    protected

    def parse_args(args)
      return [args, {}] unless opts
      hash = opts.parse(args)
      list = opts.non_opts
      [list, hash]
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
jherdman-thor-0.9.5 lib/thor/task.rb
wycats-thor-0.9.5 lib/thor/task.rb
wycats-thor-0.9.6 lib/thor/task.rb
thor-0.9.5 lib/thor/task.rb
thor-0.9.6 lib/thor/task.rb