class DotEnv

Attributes

filename[R]

Public Class Methods

new(filename = nil) click to toggle source
# File lib/dot_example/dot_env.rb, line 2
def initialize(filename = nil)
  @filename = filename || ".env"
  create_file_if_does_not_exist
end

Public Instance Methods

create_file() click to toggle source
# 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_exist() click to toggle source
# 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_lines() click to toggle source
# File lib/dot_example/dot_env.rb, line 9
def key_lines
  keys.map { |key| key + "=" }.join("\n")
end
keys() click to toggle source
# File lib/dot_example/dot_env.rb, line 20
def keys
  lines.map do |line|
    line.split("=")[0]
  end
end
lint!() click to toggle source
# 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

Private Instance Methods

lines() click to toggle source
# File lib/dot_example/dot_env.rb, line 40
def lines
  File.readlines(filename)
end