environment.rb 496B

1234567891011121314151617181920212223242526272829
  1. module Dotenv
  2. # This class inherits from Hash and represents the environemnt into which
  3. # Dotenv will load key value pairs from a file.
  4. class Environment < Hash
  5. attr_reader :filename
  6. def initialize(filename)
  7. @filename = filename
  8. load
  9. end
  10. def load
  11. update Parser.call(read)
  12. end
  13. def read
  14. File.read(@filename)
  15. end
  16. def apply
  17. each { |k, v| ENV[k] ||= v }
  18. end
  19. def apply!
  20. each { |k, v| ENV[k] = v }
  21. end
  22. end
  23. end