Class: EnvLint::DotEnvParser

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

Constant Summary collapse

COMMENT =
/\A#\s*(.*)\z/
ASSIGNMENT =
/
 \A
 (\#\s*)?          # optional variable marker
 ([\w\.]+)         # key
 =                 # separator
 (                 # optional value begin
   [^#\n]+         #   unquoted value
 )?                # value end
 \z
/x

Instance Method Summary collapse

Instance Method Details

#parse(text) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/env_lint/dot_env_parser.rb', line 3

def parse(text)
  comment_lines = []

  variables = text.lines.each_with_object([]) do |line, result|
    if match = line.strip.match(ASSIGNMENT)
      optional, name, value = match.captures
      value ||= ''
      value = value.strip.sub(/\A(['"])(.*)\1\z/, '\2')

      result << Variable.new(name, value, !!optional, comment_lines * "\n")
      comment_lines = []
    elsif match = line.strip.match(COMMENT)
      comment_lines << match.captures.first
    elsif line.strip.empty?
      comment_lines = []
    else
      raise(UnrecognizedDotEnvLine.new(line))
    end
  end

  variables
end