Class: Crew::Task::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/crew/task/arguments.rb,
lib/crew/task/arguments/dsl.rb

Defined Under Namespace

Modules: DSL Classes: BlockDefinition, Definition, Proxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArguments

Returns a new instance of Arguments.



75
76
77
# File 'lib/crew/task/arguments.rb', line 75

def initialize
  @definitions = []
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



73
74
75
# File 'lib/crew/task/arguments.rb', line 73

def definitions
  @definitions
end

Instance Method Details

#define(arg, type, desc, opts) ⇒ Object



93
94
95
# 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) ⇒ Object



97
98
99
# File 'lib/crew/task/arguments.rb', line 97

def define_block(opts, &block)
  @block_definition = BlockDefinition.new(opts, &block)
end

#process!(args, &blk) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



101
102
103
104
105
106
107
108
# 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