Class: Docopt::ChildPattern

Inherits:
Pattern
  • Object
show all
Defined in:
lib/docopt.rb

Direct Known Subclasses

Argument, Option

Instance Attribute Summary collapse

Attributes inherited from Pattern

#children

Instance Method Summary collapse

Methods inherited from Pattern

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

Constructor Details

#initialize(name, value = nil) ⇒ ChildPattern



122
123
124
125
# File 'lib/docopt.rb', line 122

def initialize(name, value=nil)
  @name = name
  @value = value
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



120
121
122
# File 'lib/docopt.rb', line 120

def name
  @name
end

#valueObject

Returns the value of attribute value.



120
121
122
# File 'lib/docopt.rb', line 120

def value
  @value
end

Instance Method Details

#flatObject



131
132
133
# File 'lib/docopt.rb', line 131

def flat
  [self]
end

#inspectObject



127
128
129
# File 'lib/docopt.rb', line 127

def inspect()
  "#{self.class.name}(#{self.name}, #{self.value})"
end

#match(left, collected = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/docopt.rb', line 136

def match(left, collected=nil)
  collected ||= []
  pos, match = self.single_match(left)
  if match == nil
    return [false, left, collected]
  end

  left_ = left.dup
  left_.slice!(pos)

  same_name = collected.select { |a| a.name == self.name }
  if @value.is_a? Array or @value.is_a? Integer
    increment = @value.is_a?(Integer) ? 1 : [match.value]
    if same_name.count == 0
      match.value = increment
      return [true, left_, collected + [match]]
    end
    same_name[0].value += increment
    return [true, left_, collected]
  end
  return [true, left_, collected + [match]]
end