Class: DocoptNG::Either

Inherits:
ParentPattern show all
Defined in:
lib/docopt_ng/either.rb

Instance Attribute Summary

Attributes inherited from ParentPattern

#children

Attributes inherited from Pattern

#children

Instance Method Summary collapse

Methods inherited from ParentPattern

#flat, #initialize, #inspect

Methods inherited from Pattern

#==, #dump, #either, #fix, #fix_identities, #fix_repeating_arguments, #to_str

Constructor Details

This class inherits a constructor from DocoptNG::ParentPattern

Instance Method Details

#match(left, collected = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/docopt_ng/either.rb', line 5

def match(left, collected = nil)
  collected ||= []
  outcomes = []
  children.each do |p|
    matched, = found = p.match(left, collected)
    if matched
      outcomes << found
    end
  end

  if outcomes.count.positive?
    return outcomes.min_by do |outcome|
      outcome[1].nil? ? 0 : outcome[1].count
    end
  end

  [false, left, collected]
end