module Rake::Opt::KeywordArgs::InstanceMethods

Attributes

arg_options[RW]

Public Instance Methods

arg_hash_to_str(hash) click to toggle source
# File lib/rake/opt/keyword_args.rb, line 151
def arg_hash_to_str(hash)
  args = ''
  hash.each{ |k, v| args << "#{k}=#{v} " }
  return args.strip
end
parse_args(args) click to toggle source
# File lib/rake/opt/keyword_args.rb, line 92
def parse_args(args)
  raise Error, 'Expected only one argument'  if args.count > 1
  args = (args.first && args.first.last) ? args.first.last : ''
  parsed_args = Hashie::Mash.new
  args.split(PARAM_SEPARATOR).each do |pair|
    arg, value = pair.split('=', 2)
    parsed_args[arg] = value.blank? ? nil : value
  end

  return parsed_args
end
verify_and_merge_args(args) click to toggle source
# File lib/rake/opt/keyword_args.rb, line 146
def verify_and_merge_args(args)
  verified = verify_args(args)
  return self.parse_args(args).merge(verified)
end
verify_args(args) click to toggle source
# File lib/rake/opt/keyword_args.rb, line 104
def verify_args(args)
  return args unless self.arg_options

  parsed_args = self.parse_args(args)
  output = Hashie::Mash.new
  self.arg_options.each do |arg_name, options|
    # Extract Value if it's set, otherwise, use the default
    if parsed_args[arg_name]
      value = parsed_args[arg_name]
    else
      value = options[:default]
    end

    # Cast the argument to the proper type
    if value.nil?
    # Keep it as nil
    elsif [Fixnum, Integer].include? options[:class]
      value = value.to_i
    elsif Float == options[:class]
      value = value.to_f
    elsif Symbol == options[:class]
      value = value.to_sym
    elsif Rational == options[:class]
      value = value.to_r
    elsif [String, nil].include? options[:class]
      value = value.to_s
    elsif Boolean == options[:class]
      value = %w{ 1 true yes t }.include? value.to_s.downcase
    else
      raise UnsupportedTypeError, "Arguments of type #{options[:class]} are not supported"
    end

    # Set the output value
    output[arg_name] = value

    if options[:required] && output[arg_name].nil? && !Rake.application.options.dryrun
      raise ValidationError, "Argument :#{arg_name} is required for Task #{self.name}"
    end
  end
  return output
end

Private Instance Methods

extract_prerequisite_args(task_args, p) click to toggle source
# File lib/rake/opt/keyword_args.rb, line 158
def extract_prerequisite_args(task_args, p)
  merged_args = self.verify_and_merge_args(task_args)
  if p.arg_options
    prereq_args = Rake::TaskArguments.new(p.arg_names, [self.arg_hash_to_str(merged_args)])
    p.verify_args(prereq_args)
  else
    prereq_args = task_args.new_scope(p.arg_names)
  end
  return prereq_args
end