Module: Rake::Opt::KeywordArgs::ClassMethods

Defined in:
lib/rake/opt/keyword_args.rb

Instance Method Summary collapse

Instance Method Details

#define_parameterized_task(*args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rake/opt/keyword_args.rb', line 29

def define_parameterized_task(*args, &block)
  arg_options = Rake.application.last_args
  Rake.application.last_args = nil
  # Parse the arguments
  name = ''
  requirements = []
  case args.size
  when 1 then
    arg = args.first
    if arg.is_a? Hash
      name = arg.keys.first
      requirements = arg.values.first
    else
      name = arg.to_s
    end
  when 2 then
    name = args.first
    deps = args.last
    if deps.is_a? Hash
      if deps.keys.first.empty?
        requirements = deps.values.last
      else
        raise DefinitionError, 'Must have no arguments when defining a paramterized task'
      end
    else
      raise DefinitionError, 'Must have no arguments when defining a paramterized task'
    end
  else
    raise DefinitionError, 'Unknown argument format'
  end

  # Set the pseudo args
  arg_options_str = arg_options.keys.sort do |x,y|
    next -1 if arg_options[x][:required] && !arg_options[y][:required]
    next 1 if !arg_options[x][:required] && arg_options[y][:required]
    next 1 if arg_options[x][:default] && !arg_options[y][:default]
    next -1 if !arg_options[x][:default] && arg_options[y][:default]
    next x.to_s <=> y.to_s
  end.map do |arg|
    options = arg_options[arg]
    if options[:default] != nil
      default_str = options[:default].to_s
    elsif options[:required]
      default_str = 'REQUIRED'
    else
      default_str = 'nil'
    end
    next "#{arg}=#{default_str}"
  end.join(PARAM_SEPARATOR)
  task_args = [ name, { [arg_options_str] => requirements } ]

  task = self.__define_task_before_keyword_args__(*task_args, &verify_args_block(&block))

  task.arg_options = arg_options

  return task
end

#verify_args_blockObject



21
22
23
24
25
26
27
# File 'lib/rake/opt/keyword_args.rb', line 21

def verify_args_block
  # Flush the Print Queue at the end of each iteration
  lambda do |*args|
    task = args.first
    yield task, task.verify_args(args[1]) if block_given?
  end
end