Sha256: 25e531ab8c039fc8822b792fe4d57f55106a953e132c14cab3a11f22be356521

Contents?: true

Size: 709 Bytes

Versions: 1

Compression:

Stored size: 709 Bytes

Contents

module Dotenv
  class Environment < Hash
    def initialize(filename)
      @filename = filename
      load
    end

    def load
      read.each do |line|
        if line =~ /\A(?:export\s+)?(\w+)(?:=|: ?)(.*)\z/
          key = $1
          case val = $2
          # Remove single quotes
          when /\A'(.*)'\z/ then self[key] = $1
          # Remove double quotes and unescape string preserving newline characters
          when /\A"(.*)"\z/ then self[key] = $1.gsub('\n', "\n").gsub(/\\(.)/, '\1')
          else self[key] = val
          end
        end
      end
    end

    def read
      File.read(@filename).split("\n")
    end

    def apply
      each { |k,v| ENV[k] ||= v }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dotenv-0.7.0 lib/dotenv/environment.rb