Class: Denv::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/denv.rb

Constant Summary collapse

COMMENT_CHAR =
'#'.freeze
DELIMITER =
'='.freeze
WHITE_SPACES =
/\s/

Instance Method Summary collapse

Constructor Details

#initialize(io, filename) ⇒ Parser

Returns a new instance of Parser.



81
82
83
84
# File 'lib/denv.rb', line 81

def initialize(io, filename)
  @io = io
  @filename = filename
end

Instance Method Details

#parseObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/denv.rb', line 86

def parse
  env = {}

  @io.each_line.with_index do |line, i|
    line = line.chomp.lstrip
    if !line.empty? && !line.start_with?(COMMENT_CHAR)
      key, value = line.split(DELIMITER, 2)
      if key && value
        if key.match(WHITE_SPACES)
          raise InvalidKeyNameError.new(line, @filename, i + 1)
        else
          env[key] = value
        end
      else
        raise InvalidFormatError.new(line, @filename, i + 1)
      end
    end
  end

  env
end