class Dotenv::Init::CommentAwareParser

Constants

COMMENT_OR_BLANK_LINE
LINE

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/dotenv/init/comment_aware_parser.rb, line 31
def initialize(*args)
  @line_number = 0
  @comments = []
  super
end

Public Instance Methods

call() click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 37
def call
  @string.split(/[\n\r]/).each do |line|
    parse_line(line)
    @line_number += 1
  end
  @hash
end

Private Instance Methods

exports_nothing?(line) click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 95
def exports_nothing?(line)
  line.split.first == "export" && variable_not_set?(line)
end
flush_comments() click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 89
def flush_comments
  comments = @comments
  @comments = []
  comments.compact.map(&:chomp).join(" ")
end
offset_for(match, value) click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 63
def offset_for(match, value)
  return [match.offset(2)[1]] * 2 unless value

  offset = match.offset(3)

  if value.start_with?("'", '"')
    [offset[0] + 1, offset[1] - 1]
  else
    offset
  end
end
parse_line(line) click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 47
def parse_line(line)
  if match = validate_line!(line)
    key, _, value, comment = match.captures
    @comments << comment

    if key
      @hash[key] = {
        default: parse_value(value),
        comments: flush_comments,
        line_number: @line_number,
        offset: offset_for(match, value),
      }
    end
  end
end
parse_value(value) click to toggle source
Calls superclass method
# File lib/dotenv/init/comment_aware_parser.rb, line 85
def parse_value(value)
  super(value || "")
end
validate_line!(line) click to toggle source
# File lib/dotenv/init/comment_aware_parser.rb, line 75
def validate_line!(line)
  if match = line.match(LINE)
    return match
  elsif exports_nothing?(line)
    raise FormatError, "Line #{line.inspect} has an unset variable"
  elsif line !~ COMMENT_OR_BLANK_LINE
    raise FormatError, "Line #{line.inspect} doesn't match format"
  end
end