module Decanter::Parser

Public Class Methods

compose_parsers(parsers) click to toggle source

Composes multiple parsers into a single parser

# File lib/decanter/parser.rb, line 17
def compose_parsers(parsers)
  raise ArgumentError.new('expects an array') unless parsers.is_a? Array
  composed_parser = Class.new(Decanter::Parser::ComposeParser)
  composed_parser.parsers(parsers)
  composed_parser
end
parsers_for(klass_or_syms) click to toggle source
# File lib/decanter/parser.rb, line 8
def parsers_for(klass_or_syms)
  Array.wrap(klass_or_syms)
       .map { |klass_or_sym| klass_or_sym_to_str(klass_or_sym) }
       .map { |parser_str|   parser_constantize(parser_str) }
       .map { |parser|       expand(parser) }
       .flatten
end

Private Class Methods

expand(parser) click to toggle source

expand to include preparsers

# File lib/decanter/parser.rb, line 47
def expand(parser)
  Parser.parsers_for(parser.preparsers).push(parser)
end
klass_or_sym_to_str(klass_or_sym) click to toggle source

convert from a class or symbol to a string and concat 'Parser'

# File lib/decanter/parser.rb, line 27
def klass_or_sym_to_str(klass_or_sym)
  case klass_or_sym
  when Class
    klass_or_sym.name
  when Symbol
    symbol_to_string(klass_or_sym)
  else
    raise ArgumentError.new("cannot lookup parser for #{klass_or_sym} with class #{klass_or_sym.class}")
  end.concat('Parser')
end
parser_constantize(parser_str) click to toggle source

convert from a string to a constant

# File lib/decanter/parser.rb, line 39
def parser_constantize(parser_str)
  # safe_constantize returns nil if match not found
  parser_str.safe_constantize ||
    concat_str(parser_str).safe_constantize ||
    raise(NameError.new("cannot find parser #{parser_str}"))
end