Class: CmdMatcher::LineMatcher

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

Direct Known Subclasses

LineEqualer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, opt = {}) ⇒ LineMatcher

Returns a new instance of LineMatcher.



123
124
125
126
127
128
129
130
131
132
# File 'lib/cmd_matcher.rb', line 123

def initialize(matcher, opt = {})
  @names = opt[:names] || []
  @matcher = matcher
  @matches = nil
  if opt[:at_most] && opt[:at_most] > 1 or opt[:at_least]
    @has_many = true
  else
    @has_many = false
  end
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



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

def matches
  @matches
end

Instance Method Details

#append_to(matches) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/cmd_matcher.rb', line 149

def append_to(matches)
  @names.each {|name|
    if matches[name].is_a?(Array)
      matches[name] += @matches[name]
    else
      matches[name] = @matches[name]
    end
  }
end

#match(line) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cmd_matcher.rb', line 134

def match(line)
  return false unless line
  m = line.match(@matcher)
  return false unless m
  @matches = {}
  @names.each_with_index {|name, index|
    if @has_many
      @matches[name] = [m[index + 1]]
    else
      @matches[name] = m[index + 1]
    end
  }
  return m
end