Parent

Methods

Dotenv::Parser

Constants

LINE

Public Class Methods

call(string) click to toggle source
# File lib/dotenv/parser.rb, line 28
def self.call(string)
  new(string).call
end
new(string) click to toggle source
# File lib/dotenv/parser.rb, line 32
def initialize(string)
  @string = string
end

Public Instance Methods

call() click to toggle source
# File lib/dotenv/parser.rb, line 36
def call
  @string.split("\n").inject({}) do |hash, line|
    if match = line.match(LINE)
      key, value = match.captures

      value ||= ''
      # Remove surrounding quotes
      value = value.strip.sub(/\A(['"])(.*)\11\\z/, '\2')

      if $1 == '"'
        value = value.gsub('\n', "\n")
        # Unescape all characters except $ so variables can be escaped properly
        value = value.gsub(/\\([^$])/, '\1')
      end

      @@substitutions.each do |proc|
        value = proc.call(value, hash)
      end

      hash[key] = value
    elsif line !~ /\A\s*(?:#.*)?\z/ # not comment or blank line
      raise FormatError, "Line #{line.inspect} doesn't match format"
    end
    hash
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.