IN PROGRESS
Just add it to your Gemfile, and then run bundle on your project:
gem 'dynamic_parser', '0.0.2'Your Dynamic Parser may be a composite of both Stateless and Stateful parsers. To build a composite/dynamic paser, first you need one or more MicroParsers.
The following samples will expect a text/String as the received artifact to be parsed, but you're free to use any kind of object/content.
# parses the artifact statically
class StatelessParser
include DynamicParser::MicroParser::Stateless
def self.detect(txt)
txt.include?('static')
end
def self.parse(txt)
tokens = []
txt.split(' ').each_with_index do |piece, i|
tokens.push OpenStruct.new(index: i, content: piece)
end
tokens
end
endclass StatefulParser
include DynamicParser::MicroParser::Stateful
def initialize
@data = []
end
def detect(txt)
self.class.detect(txt)
end
def self.detect(txt)
if txt.match('stateful') == nil
false
else
true
end
end
def parse!(txt)
tokens = []
txt.split(' ').each_with_index do |piece, i|
@data.push OpenStruct.new(index: i, content: piece)
end
# you could be doing some IO here, like writing to a file, database, http...
end
def gimme_data
@data
end
endWhen running a composite parser, all MicroParsers found at .micro_parsers will be filtered through the .detect method, and then called .parse/#parse.
class CompositeParser
include DynamicParser::Parser
def self.micro_parsers
[StatefulParser, StatelessParser]
end
end# run you dynamic parser
CompositeParser.parse do |micro_parser, output|
case
when micro_parser==StatelessParser
# do something with output here
end
endYou should check the factories to learn what you need to build your objects, and the tests to learn how to use them. But hey, we have docs right here.
- ...;
Just fork DynamicParser, add your feature+spec, and make a pull request.
Please see LICENSE for licensing details.

