module DynamicParser::Parser::ClassMethods

Public Instance Methods

build(txt) click to toggle source

filters the microparsers list for the given content

# File lib/dynamic_parser/parser.rb, line 4
def build(txt)
  parsers = []
  self.micro_parsers.each do |parser|
    parsers << parser if parser.detect(txt)
  end
  parsers
end
micro_parsers() click to toggle source
# File lib/dynamic_parser/parser.rb, line 30
def micro_parsers
  raise 'This method should return an Array of MicroParsers'
end
parse(txt, &block) click to toggle source
# File lib/dynamic_parser/parser.rb, line 12
def parse(txt, &block)
  parsers = build(txt)
  parsers.each do |micro_parser|
    if micro_parser.ancestors.include?(DynamicParser::MicroParser::Stateful)
      micro_parser.new.parse!(txt)
    elsif micro_parser.ancestors.include?(DynamicParser::MicroParser::Stateless)
      if block_given?
        block.call(micro_parser, micro_parser.parse(txt))
      else
        raise 'You must give a block for Stateless parsers' 
      end
    else
      raise "#{micro_parser} is not a valid MicroParser!"
    end  
  end
  parsers.size
end