Module: EnvParser

Defined in:
lib/cocos/env.rb

Overview

todo/check:

move to its own (standandaloen) envparser gem - why? why not?

Defined Under Namespace

Classes: Error, ParseError

Constant Summary collapse

LINE_RX =

todo/check - if support for empty values e.g. abc= is required/possible???

todo/ addd support for quoted values - why? why not?
add support for "inline" end of line comments - why? why not?
add support for escapes and multi-line values - why? why not?
/\A(?<key>[A-Za-z][A-Za-z0-9_-]*)
    [ ]*
      =
    [ ]*
  (?<value>.+?)    ## non-greedy
 \z
/x

Class Method Summary collapse

Class Method Details

.load(text) ⇒ Object



21
# File 'lib/cocos/env.rb', line 21

def self.load( text )   parse( text ); end

.load_file(path) ⇒ Object

returns a hash

(compatible structure - works like YAML.load_file)

 change to .read(path) and parse( text) - why? why not?


17
18
19
20
# File 'lib/cocos/env.rb', line 17

def self.load_file( path )
    text = File.open( path, 'r:utf-8' ) { |f| f.read }
    parse( text )
end

.parse(text) ⇒ Object

use a parser class - why? why not?



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cocos/env.rb', line 44

def self.parse( text )
   h = {}

   lineno = 0   
  text.each_line do |line|
    lineno += 1    ## track line numbers for (parse) error reporting

    line = line.strip   ## check: use strip (or be more strict) - why? why not?
    ## skip empty and comment lines
    next if line.empty? || line.start_with?( '#' )

    if m=LINE_RX.match(line)
      key   = m[:key]
      value = m[:value]
      
      ## todo/check - check/warn about duplicates - why? why not?
      h[key] = value
   else 
      raise ParseError,  "line #{lineno} - unknown line type; cannot parse >#{line}<"
   end
  end
  h
end