class Matchdoc::Matcher

Attributes

failed_chunk[R]

Public Class Methods

new(subject) click to toggle source
# File lib/matchdoc/matcher.rb, line 15
def initialize(subject)
  scanner = StringScanner.new(subject)
  @match_chunks = []
  until scanner.eos?
    break unless scanner.scan_until(/\(\?[-mix]{4}:/) #regex prefix
    @match_chunks << Regexp.new(Regexp.escape(scanner.pre_match))
    regex_sub = /\((?>[^()]|(\g<0>))*\)/.match(scanner.matched+scanner.post_match).to_s
    @match_chunks << Regexp.new(regex_sub)
    scanner.pos = scanner.pos + (regex_sub.length - 7)
    scanner.string = scanner.rest
  end
end

Public Instance Methods

=~(subject) click to toggle source
# File lib/matchdoc/matcher.rb, line 40
def =~(subject)
  begin
    match!(subject)
  rescue FailedChunk
    false
  else
    true
  end
end
match!(subject) click to toggle source
# File lib/matchdoc/matcher.rb, line 28
def match!(subject)
  scanner = StringScanner.new(subject)
  scanned_chunks = []
  @match_chunks.each.with_index do |chunk, i|
    unless scanned_chunks[i] = scanner.scan(chunk)
      raise FailedChunk.new(scanner, chunk)
    end
  end

  scanned_chunks
end
to_s() click to toggle source
# File lib/matchdoc/matcher.rb, line 50
def to_s
  @match_chunks.map { |i| '/' + YAML.load(%Q(---\n"#{i.source}"\n)) + '/' }.join
end