Method: Tap::Declarations#task

Defined in:
lib/tap/declarations.rb

#task(*args, &block) ⇒ Object



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
86
# File 'lib/tap/declarations.rb', line 56

def task(*args, &block)
  const_name, configs, dependencies, arg_names = resolve_args(args)
  task_class = declare(const_name, configs, dependencies) do |*inputs|
    # collect inputs to make a rakish-args object
    args = {}
    arg_names.each do |arg_name|
      break if inputs.empty?
      args[arg_name] = inputs.shift
    end
    args = OpenStruct.new(args)
    
    # execute each block assciated with this task
    self.class::BLOCKS.each do |task_block|
      case task_block.arity
      when 0 then task_block.call()
      when 1 then task_block.call(self)
      else task_block.call(self, args)
      end
    end
    
    nil
  end
  register_doc(task_class, arg_names)
  
  # add the block to the task
  unless task_class.const_defined?(:BLOCKS)
    task_class.const_set(:BLOCKS, [])
  end
  task_class::BLOCKS << block unless block == nil
  task_class.instance
end