Class: Greenhouse::Resources::DotenvFile

Inherits:
Object
  • Object
show all
Includes:
FileResource
Defined in:
lib/greenhouse/resources/dotenv_file.rb

Defined Under Namespace

Classes: ConfigVars

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileResource

included

Constructor Details

#initialize(path) ⇒ DotenvFile

Returns a new instance of DotenvFile.



63
64
65
66
# File 'lib/greenhouse/resources/dotenv_file.rb', line 63

def initialize(path)
  super
  reload
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/greenhouse/resources/dotenv_file.rb', line 6

def config
  @config
end

Instance Method Details

#reloadObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/greenhouse/resources/dotenv_file.rb', line 30

def reload
  @config = ConfigVars.new
  return @config unless exists?
  # pulled this straight from Dotenv, too bad there's no simple Dotenv.parse method in the gem
  read do |line|
    if line =~ /\A(?:export\s+)?([\w\.]+)(?:=|: ?)(.*)\z/
      key = $1
      case val = $2
      # Remove single quotes
      when /\A'(.*)'\z/ then @config[key] = $1
      # Remove double quotes and unescape string preserving newline characters
      when /\A"(.*)"\z/ then @config[key] = $1.gsub('\n', "\n").gsub(/\\(.)/, '\1')
      else @config[key] = val
      end
    end
  end
  @config
end


58
59
60
61
# File 'lib/greenhouse/resources/dotenv_file.rb', line 58

def unlink
  super
  reload
end

#writeObject



49
50
51
52
53
54
55
56
# File 'lib/greenhouse/resources/dotenv_file.rb', line 49

def write
  open("w") do |f|
    @config.each do |key,val|
      next if val.nil? || val.to_s.empty?
      f.write("#{key}=#{val.to_s}\n")
    end
  end
end