module Dotenv
Extended Modules
Defined in:
dotenv.crparser.cr
Instance Method Summary
-
#build(env_vars : Hash(String, String), value_quotes : Quotes = Quotes::None) : String
Builds a
.env
formatted string, and escape special characters in values. -
#build(io, env_vars : Hash(String, String), value_quotes : Quotes = Quotes::None) : Nil
Builds a
.env
formatted data to theIO
, and quotes to put around the value. -
#load(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String)
Loads environment variables from a file into the
ENV
constant. -
#load(io : IO, override_keys : Bool = false) : Hash(String, String)
Loads environment variables from an
IO
into theENV
constant. -
#load(hash : Hash(String, String), override_keys : Bool = false) : Hash(String, String)
Loads a Hash of environment variables into the
ENV
constant. -
#load?(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String) | Nil
Loads environment variables from a file into the
ENV
constant if the file is present, else returnsnil
. -
#load_string(env_vars : String, override_keys : Bool = false) : Hash(String, String)
Loads environment variables from a
String
into theENV
constant. -
#parse(env_vars : String | IO) : Hash(String, String)
Parses a
.env
formattedString
/IO
data, and returns a hash (without loading it toENV
).
Instance Method Detail
Builds a .env
formatted string, and escape special characters in values.
Only variable key characters are validated.
require "dotenv"
Dotenv.build({"VAR" => "Hello"}) => "VAR=Hello"
Builds a .env
formatted data to the IO
, and quotes to put around the value.
Only variable key characters are validated.
require "dotenv"
File.open "w", ".env" do |io|
Dotenv.build(io, {"VAR" => "Hello"})
end
Loads environment variables from a file into the ENV
constant.
require "dotenv"
File.write ".env-file", "VAR=Hello"
Dotenv.load ".env-file" # => {"VAR" => "Hello"}
Dotenv.load ".absent-file" # => No such file or directory (File::NotFoundError)
Loads environment variables from an IO
into the ENV
constant.
require "dotenv"
hash = Dotenv.load IO::Memory.new("VAR=Hello")
hash # => {"VAR" => "Hello"}
Loads a Hash of environment variables into the ENV
constant.
require "dotenv"
Dotenv.load({"VAR" => "test"})
Loads environment variables from a file into the ENV
constant
if the file is present, else returns nil
.
require "dotenv"
File.write ".env-file", "VAR=Hello"
Dotenv.load? ".env-file" # => {"VAR" => "Hello"}
Dotenv.load? ".not-present" # => nil
Loads environment variables from a String
into the ENV
constant.
require "dotenv"
hash = Dotenv.load_string "VAR=Hello"
hash # => {"VAR" => "Hello"}
Parses a .env
formatted String
/IO
data, and returns a hash (without loading it to ENV
).
require "dotenv"
hash = Dotenv.parse "VAR=Hello"
hash # => {"VAR" => "Hello"}