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_repeating_arguments, #to_str

Constructor Details

#initialize(name, value = nil) ⇒ ChildPattern

Returns a new instance of ChildPattern.



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

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



129
130
131
# File 'lib/docopt.rb', line 129

def name
  @name
end

#valueObject

Returns the value of attribute value.



129
130
131
# File 'lib/docopt.rb', line 129

def value
  @value
end

Instance Method Details

#flat(*types) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/docopt.rb', line 140

def flat(*types)
  if types.empty? or types.include?(self.class)
    [self]
  else
    []
  end
end

#inspectObject



136
137
138
# File 'lib/docopt.rb', line 136

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

#match(left, collected = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/docopt.rb', line 149

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
    if @value.is_a? Integer
      increment = 1
    else
      increment = match.value.is_a?(String) ? [match.value] : match.value
    end
    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