Sha256: 65f75522e5d7d1ed563a13fdca3fffec0697935aff5655d1854c69f8d39bc887

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

module Nori

  # = Nori::Parser
  #
  # Manages the parser classes. Currently supports:
  #
  # * REXML
  # * Nokogiri
  module Parser

    # The default parser.
    DEFAULT = :rexml

    # List of available parsers.
    PARSERS = { :rexml => "REXML", :nokogiri => "Nokogiri" }

    # Returns the parser to use. Defaults to <tt>Nori::Parser::REXML</tt>.
    def self.use
      @use ||= DEFAULT
    end

    # Sets the +parser+ to use. Raises an +ArgumentError+ unless the +parser+ exists.
    def self.use=(parser)
      validate_parser! parser
      @use = parser
    end

    # Returns the parsed +xml+ using the parser to use. Raises an +ArgumentError+
    # unless the optional or default +parser+ exists.
    def self.parse(xml, parser = nil)
      load_parser(parser).parse xml
    end

  private

    # Raises an +ArgumentError+ unless the +parser+ exists.
    def self.validate_parser!(parser)
      raise ArgumentError, "Invalid Nori parser: #{parser}" unless PARSERS[parser]
    end

    # Requires and returns the +parser+ to use.
    def self.load_parser(parser)
      parser ||= use
      validate_parser! parser

      require "nori/parser/#{parser}"
      const_get PARSERS[parser]
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
nori-1.0.3 lib/nori/parser.rb
nori-1.0.2 lib/nori/parser.rb
search_biomodel-1.0.0 search_biomodel/ruby/1.8/gems/nori-1.0.1/lib/nori/parser.rb
nori-0.2.4 lib/nori/parser.rb
nori-1.0.1 lib/nori/parser.rb
nori-1.0.0 lib/nori/parser.rb
nori-0.2.3 lib/nori/parser.rb
nori-0.2.2 lib/nori/parser.rb
nori-0.2.1 lib/nori/parser.rb
nori-0.2.0 lib/nori/parser.rb