class HttpFind::Matcher

Constants

MATCH_COLOR

Attributes

text[R]

Public Class Methods

new(text) click to toggle source
# File lib/http_find/matcher.rb, line 10
def initialize(text)
  @text = text
end

Public Instance Methods

match(subj) click to toggle source
# File lib/http_find/matcher.rb, line 14
def match(subj)
  if subj.class == Regexp
    regexp_match(subj)
  else
    string_match(subj.to_s)
  end
end

Private Instance Methods

base_match() { |line, i, matches| ... } click to toggle source
# File lib/http_find/matcher.rb, line 28
def base_match
  matches = []

  lines.each_with_index do |line, i|
    yield(line, i, matches)
  end

  matches
end
lines() click to toggle source
# File lib/http_find/matcher.rb, line 24
def lines
  @text.split("\n")
end
regexp_match(regexp) click to toggle source
# File lib/http_find/matcher.rb, line 38
def regexp_match(regexp)
  base_match do |line, i, matches|
    regexp_matches = line.scan(regexp)
    unless regexp_matches.empty?
      regexp_matches.each { |m| line.sub!(m, m.colorize(MATCH_COLOR)) }
      matches << result(i + 1, line)
    end
  end
end
result(line_num, text) click to toggle source
# File lib/http_find/matcher.rb, line 56
def result(line_num, text)
  {
    line: line_num,
    text: text
  }
end
string_match(str) click to toggle source
# File lib/http_find/matcher.rb, line 48
def string_match(str)
  base_match do |line, i, matches|
    if line.include?(str)
      matches << result(i + 1, line.gsub(str, str.colorize(MATCH_COLOR)))
    end
  end
end