Class: Dotenv::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dotenv/parser.rb

Overview

This class enables parsing of a string for key value pairs to be returned and stored in the Environment. It allows for variable substitutions and exporting of variables.

Constant Summary collapse

LINE =
/
  \A
  \s*
  (?:export\s+)?    # optional export
  ([\w\.]+)         # key
  (?:\s*=\s*|:\s+?) # separator
  (                 # optional value begin
    '(?:\'|[^'])*'  #   single quoted value
    |               #   or
    "(?:\"|[^"])*"  #   double quoted value
    |               #   or
    [^#\n]+         #   unquoted value
  )?                # value end
  \s*
  (?:\#.*)?         # optional comment
  \z
/x

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



40
41
42
43
# File 'lib/dotenv/parser.rb', line 40

def initialize(string)
  @string = string
  @hash = {}
end

Class Attribute Details

.substitutionsObject (readonly)

Returns the value of attribute substitutions.



33
34
35
# File 'lib/dotenv/parser.rb', line 33

def substitutions
  @substitutions
end

Class Method Details

.call(string) ⇒ Object



35
36
37
# File 'lib/dotenv/parser.rb', line 35

def call(string)
  new(string).call
end

Instance Method Details

#callObject



45
46
47
48
49
50
# File 'lib/dotenv/parser.rb', line 45

def call
  @string.split(/[\n\r]+/).each do |line|
    parse_line(line)
  end
  @hash
end