Class: DotenvUtil

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

Overview

Provides Generic support for manipulating Dotenv files

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_file) ⇒ DotenvUtil

Returns a new instance of DotenvUtil.



26
27
28
29
# File 'lib/dotenv_util.rb', line 26

def initialize(env_file)
  @env_text = env_file
  @env = parse_env_file
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



25
26
27
# File 'lib/dotenv_util.rb', line 25

def env
  @env
end

#env_textObject (readonly)

Returns the value of attribute env_text.



25
26
27
# File 'lib/dotenv_util.rb', line 25

def env_text
  @env_text
end

Instance Method Details

#generate_envObject



35
36
37
38
39
40
# File 'lib/dotenv_util.rb', line 35

def generate_env
  env.collect do |key, val|
    val = %("#{val}") if val =~ /\s/
    "#{key}=#{val}"
  end.join("\n")
end

#set(target, value) ⇒ Object



31
32
33
# File 'lib/dotenv_util.rb', line 31

def set(target, value)
  env[target] = value
end