Parent

Class/Module Index [+]

Quicksearch

Dotenv::Environment

Constants

LINE
VARIABLE

Public Class Methods

new(filename) click to toggle source
# File lib/dotenv/environment.rb, line 30
def initialize(filename)
  @filename = filename
  load
end

Public Instance Methods

apply() click to toggle source
# File lib/dotenv/environment.rb, line 72
def apply
  each { |k,v| ENV[k] ||= v }
end
load() click to toggle source
# File lib/dotenv/environment.rb, line 35
def load
  read.each do |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

      # Process embedded variables
      value.scan(VARIABLE).each do |parts|
        if parts.first == '\'
          replace = parts[1...-1].join('')
        else
          replace = self.fetch(parts.last) { ENV[parts.last] }
        end

        value = value.sub(parts[0...-1].join(''), replace || '')
      end

      self[key] = value
    elsif line !~ /\A\s*(?:#.*)?\z/ # not comment or blank line
      raise FormatError, "Line #{line.inspect} doesn't match format"
    end
  end
end
read() click to toggle source
# File lib/dotenv/environment.rb, line 68
def read
  File.read(@filename).split("\n")
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.