Class: Dotenv::Parser
- Inherits:
-
Object
- Object
- Dotenv::Parser
- Defined in:
- lib/dotenv/parser.rb
Overview
Parses the ‘.env` file format into key/value pairs. It allows for variable substitutions, command substitutions, and exporting of variables.
Constant Summary collapse
- LINE =
/ (?:^|\A) # beginning of line \s* # leading whitespace (?<export>export\s+)? # optional export (?<key>[\w.]+) # key (?: # optional separator and value (?:\s*=\s*?|:\s+?) # separator (?<value> # optional value begin \s*'(?:\\'|[^'])*' # single quoted value | # or \s*"(?:\\"|[^"])*" # double quoted value | # or [^\#\n]+ # unquoted value )? # value end )? # separator and value end \s* # trailing whitespace (?:\#.*)? # optional comment (?:$|\z) # end of line /x
Class Attribute Summary collapse
-
.substitutions ⇒ Object
readonly
Returns the value of attribute substitutions.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(string, overwrite: false) ⇒ Parser
constructor
A new instance of Parser.
Constructor Details
#initialize(string, overwrite: false) ⇒ Parser
Returns a new instance of Parser.
44 45 46 47 48 49 |
# File 'lib/dotenv/parser.rb', line 44 def initialize(string, overwrite: false) # Convert line breaks to same format @string = string.gsub(/[\n\r]+/, "\n") @hash = {} @overwrite = overwrite end |
Class Attribute Details
.substitutions ⇒ Object (readonly)
Returns the value of attribute substitutions.
37 38 39 |
# File 'lib/dotenv/parser.rb', line 37 def substitutions @substitutions end |
Class Method Details
.call ⇒ Object
39 40 41 |
# File 'lib/dotenv/parser.rb', line 39 def call(...) new(...).call end |
Instance Method Details
#call ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dotenv/parser.rb', line 51 def call @string.scan(LINE) do match = $LAST_MATCH_INFO if existing?(match[:key]) # Use value from already defined variable @hash[match[:key]] = ENV[match[:key]] elsif match[:export] && !match[:value] # Check for exported variable with no value if !@hash.member?(match[:key]) raise FormatError, "Line #{match.to_s.inspect} has an unset variable" end else @hash[match[:key]] = parse_value(match[:value] || "") end end @hash end |