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.



92
93
94
95
96
97
98
99
100
101
# File 'lib/cmd-matcher.rb', line 92

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.



91
92
93
# File 'lib/cmd-matcher.rb', line 91

def matches
  @matches
end

Instance Method Details

#append_to(matches) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/cmd-matcher.rb', line 117

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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cmd-matcher.rb', line 103

def match(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 true
end