module Decanter::Parser
Public Class Methods
Source
# 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
Composes multiple parsers into a single parser
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
Source
# File lib/decanter/parser.rb, line 47 def expand(parser) Parser.parsers_for(parser.preparsers).push(parser) end
expand to include preparsers
Source
# 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
convert from a class or symbol to a string and concat ‘Parser’
Source
# 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
convert from a string to a constant