Class: Envs::EnvFile

Inherits:
Hash
  • Object
show all
Defined in:
lib/envs/env_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EnvFile

Returns a new instance of EnvFile.



5
6
7
8
# File 'lib/envs/env_file.rb', line 5

def initialize(path)
  @path = path
  parse if exist?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/envs/env_file.rb', line 3

def path
  @path
end

Instance Method Details

#[]=(key, value) ⇒ Object



24
25
26
27
28
29
# File 'lib/envs/env_file.rb', line 24

def []=(key, value)
  super(key, value)
  File.open(@path, "a") do |f|
    f << "#{key}=#{value}\n"
  end
end

#parseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/envs/env_file.rb', line 10

def parse
  new_hash = {}
  File.open(@path).each_line do |line|
    case line
    when /^\s*$/, /^#/ # ignore
    else
      line.chomp
      key, value = line.split('=', 2)
      new_hash[key] = value.strip
    end
  end
  update(new_hash)
end