Skip to content

ContaBoa/dynamic-parser

Repository files navigation

Code Climate Code Climate Codeship Status for ContaBoa/dynamic_parser

Dynamic Parser

IN PROGRESS

Install

Just add it to your Gemfile, and then run bundle on your project:

  gem 'dynamic_parser', '0.0.2'

Use

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.

Stateless MicroParser

# 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
end

Stateful MicroParser

class 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
end

Composite Parser

When 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
end

Docs

You 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.

Roadmap

  • ...;

Contribute

Just fork DynamicParser, add your feature+spec, and make a pull request.

License

Please see LICENSE for licensing details.

About

An interchangeble, composable parser builder for Ruby.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages