environment.rb 632 B

12345678910111213141516171819202122232425
  1. module Dotenv
  2. # A `.env` file that will be read and parsed into a Hash
  3. class Environment < Hash
  4. attr_reader :filename, :overwrite
  5. # Create a new Environment
  6. #
  7. # @param filename [String] the path to the file to read
  8. # @param overwrite [Boolean] whether the parser should assume existing values will be overwritten
  9. def initialize(filename, overwrite: false)
  10. super()
  11. @filename = filename
  12. @overwrite = overwrite
  13. load
  14. end
  15. def load
  16. update Parser.call(read, overwrite: overwrite)
  17. end
  18. def read
  19. File.open(@filename, "rb:bom|utf-8", &:read)
  20. end
  21. end
  22. end