class Booby::Parser
Public: Generic configuration parser. You shouldn’t access this class directly, use Booby#open and Booby#prepare instead.
Public Class Methods
new(source)
click to toggle source
Public: Initializer.
source - The String source code of a config file.
# File lib/booby/parser.rb, line 9 def initialize(source) @source = source end
Public Instance Methods
parse_with(processor)
click to toggle source
Public: Parses initialized config source with using given processor class. The processor class must implement process instance method.
processor - The processor class to parse source with.
Returns processor instance that contains parsed data.
# File lib/booby/parser.rb, line 19 def parse_with(processor) processor.new.tap do |p| @__root_processor = @__current_processor = p @source.split(/$/).each_with_index { |line, i| process_line(line, i) } end end
Private Instance Methods
process_line(line, line_no)
click to toggle source
Internal: Processes single line from source file.
line - The String line to be processed. line_no - The Integer current line number.
Returns nothing.
# File lib/booby/parser.rb, line 34 def process_line(line, line_no) line.strip! return if line =~ /^\#/ # Skip comment lines... if line.empty? # Skip empty line and close recent processing group (if any)... @__current_processor = @__root_processor return end cmd, *args = line.split # Check arity for this command... ar = @__current_processor.class.const_get(:ARITY) rescue nil ar = ar[cmd] if ar ar_int = (ar.to_i rescue 0) if (ar === true && args.empty?) || (ar_int > 0 && args.size != ar_int) raise InvalidNumberOfParametersError.new(ar, args.size, cmd, line_no) end # Parse... res = @__current_processor.process(line_no, cmd, *args) # Check result... case res when true return when false raise UnsupportedOptionError.new(cmd, line_no) else unless res.respond_to?(:process) raise InvalidResponseError.new(res) end @__current_processor = res end end