class EnvLint::DotEnvParser

Constants

ASSIGNMENT
COMMENT

Public Instance Methods

parse(text) click to toggle source
# 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