Class: Command::Repeating

Inherits:
ArgumentDecoration show all
Defined in:
lib/command-set/arguments.rb

Overview

Consumes several positions of the decorated argument.

repeating.file_argument :some_files

Will collect an array into some_files of validated files.

Instance Attribute Summary

Attributes inherited from Argument

#name

Instance Method Summary collapse

Methods inherited from ArgumentDecoration

#decorated, #initialize, #pretty_print_instance_variables, register

Methods inherited from Argument

#basis, #check_present, #complete, #initialize, #names, #omittable?, #parse, register, #required?, #subject_requirements, #validate

Constructor Details

This class inherits a constructor from Command::ArgumentDecoration

Instance Method Details

#consume(subject, arguments) ⇒ Object



547
548
549
550
551
552
553
# File 'lib/command-set/arguments.rb', line 547

def consume(subject, arguments)
  result = {}
  until arguments.empty? or not validate(arguments.first, subject) do
    merge_hashes(result, decorated.consume(subject, arguments))
  end
  return result
end

#consume_hash(subject, hash) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/command-set/arguments.rb', line 533

def consume_hash(subject, hash)
  result = [*names].inject({}) do |arg_hash, name|
    terms = hash[name]
    if terms.nil?
      arg_hash
    else
      [*terms].inject(arg_hash) do |a_h, term|
        merge_hashes(a_h, decorated.consume_hash(subject, {name => term}))
      end
    end
  end
  return result
end

#match_terms(subject, terms, arguments) ⇒ Object



555
556
557
558
559
560
561
562
563
564
# File 'lib/command-set/arguments.rb', line 555

def match_terms(subject, terms, arguments)
  old_length = terms.length + 1
  while terms.length < old_length and not terms.empty? do
    old_length = terms.length
    decorated.match_terms(subject, terms, arguments.dup)
  end 

  arguments.shift
  return true
end

#merge_hashes(hash, other_hash) ⇒ Object



566
567
568
569
570
571
572
# File 'lib/command-set/arguments.rb', line 566

def merge_hashes(hash, other_hash)
  other_hash.each_pair do |name, value|
    hash[name] ||= []
    hash[name] << value
  end
  return hash
end