Class: DotEnv

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ DotEnv

Returns a new instance of DotEnv.



2
3
4
5
# File 'lib/dot_example/dot_env.rb', line 2

def initialize(filename = nil)
  @filename = filename || ".env"
  create_file_if_does_not_exist
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/dot_example/dot_env.rb', line 7

def filename
  @filename
end

Instance Method Details

#create_fileObject



32
33
34
35
36
# File 'lib/dot_example/dot_env.rb', line 32

def create_file
  %x[ touch #{filename} ]
  puts Paint[".env created", :green]
  # TODO: Add to .gitignore if it's not there already.
end

#create_file_if_does_not_existObject



26
27
28
29
30
# File 'lib/dot_example/dot_env.rb', line 26

def create_file_if_does_not_exist
  unless Dir.glob(filename).any?
    create_file
  end
end

#key_linesObject



9
10
11
# File 'lib/dot_example/dot_env.rb', line 9

def key_lines
  keys.map { |key| key + "=" }.join("\n")
end

#keysObject



20
21
22
23
24
# File 'lib/dot_example/dot_env.rb', line 20

def keys
  lines.map do |line|
    line.split("=")[0]
  end
end

#lint!Object



13
14
15
16
17
18
# File 'lib/dot_example/dot_env.rb', line 13

def lint!
  unless line.match(/^[^=\s]*=[^=\s]*$/)
    # TODO: Don't know if this is the best error message
      raise "Invalid Environment variable defintion"
  end
end