class Crew::Task::Arguments

Attributes

definitions[R]

Public Class Methods

new() click to toggle source
# File lib/crew/task/arguments.rb, line 75
def initialize
  @definitions = []
end

Public Instance Methods

define(arg, type, desc, opts) click to toggle source
# File lib/crew/task/arguments.rb, line 93
def define(arg, type, desc, opts)
  @definitions << Definition.new(arg, type, desc, opts)
end
define_block(opts, &block) click to toggle source
# File lib/crew/task/arguments.rb, line 97
def define_block(opts, &block)
  @block_definition = BlockDefinition.new(opts, &block)
end
process!(args, &blk) click to toggle source
# File lib/crew/task/arguments.rb, line 79
def process!(args, &blk)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  opts = opts.each_with_object({}){|(k,v), h| h[k.to_s] = v}
  if args.size < minimum_args or (maximum_args && args.size > maximum_args)
    message = "wrong number of arguments (got #{args.size} expected at least #{minimum_args})"
    message << " and at most #{maximum_args}" if maximum_args
    raise ArgumentError, message
  end
  @args = args
  @opts = opts
  @block = @block_definition.process(&blk) if @block_definition
  raise if blk && @block_definition.nil?
end
proxy(task) click to toggle source
# File lib/crew/task/arguments.rb, line 101
def proxy(task)
  args = @args.clone
  @proxy ||= begin
    params = {}
    @definitions.each { |definition| params[definition.name.to_s] = definition.value(args, @opts) }
    Proxy.new(task, params, @block)
  end
end

Private Instance Methods

glob_count() click to toggle source
# File lib/crew/task/arguments.rb, line 123
def glob_count
  @definitions.count {|a| a.glob? }
end
maximum_args() click to toggle source
# File lib/crew/task/arguments.rb, line 115
def maximum_args
  glob_count > 0 ? nil : (total_count - glob_count)
end
minimum_args() click to toggle source
# File lib/crew/task/arguments.rb, line 111
def minimum_args
  required_count
end
required_count() click to toggle source
# File lib/crew/task/arguments.rb, line 119
def required_count
  @definitions.count {|a| a.required? }
end
total_count() click to toggle source
# File lib/crew/task/arguments.rb, line 127
def total_count
  @definitions.count
end