A very simple environment loader for ruby

There are many gems which do app configuration loading for ruby. However, you don’t really need a gem to do environment loading. Here is a snippet of code which does most of what you want.

1
2
3
4
5
6
7
class EnvLoader
def load(path)
YAML.load_file(path).each do |k, v|
ENV[k] = v.to_s
end
end
end

And put this at the top of your application

1
2
require_relative '../app/classes/env_loader.rb'
EnvLoader.new.load(File.expand_path('../../env', __FILE__))

Here are some specs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# specs for it
require 'rails_helper'

describe EnvLoader do
describe '#load' do
it 'imports stuff into ENV' do
temp = "/tmp/#{Time.now.to_i}"
File.write(temp, <<-EOS.strip_heredoc)
SECRET: This is awesome
FOO: 33
EOS

EnvLoader.new.load(temp)
expect(ENV['FOO']).to eq('33')
expect(ENV['SECRET']).to eq("This is awesome")
end
end
end

I am currently working on LiveForm which makes setting up contact forms on your website a breeze.