dotenv 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.env ADDED
@@ -0,0 +1 @@
1
+ DOTENV=true
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # dotenv
2
2
 
3
- Loads environment settings for your application from `.env`.
3
+ Loads environment variables from `.env` into `ENV`, automagically.
4
4
 
5
- The emerging practice of storing application configuration in environment variables is a great idea, but it's not always practical to set all of those environment variables in your development or continuous integration environments. [Foreman](https://github.com/ddollar/foreman) provides this handy feature of loading settings from `.env`, which works great for anything that you want to put in your `Procfile`. But it makes things difficult when you want to run a console or rake task. `dotenv` solves that problem.
5
+ Read more about the [motivation for dotenv at opensoul.org](http://opensoul.org/blog/archives/2012/07/24/dotenv/).
6
6
 
7
7
  ## Installation
8
8
 
@@ -41,6 +41,11 @@ Add your application configuration to `.env`.
41
41
  S3_BUCKET=dotenv
42
42
  SECRET_KEY=sssshhh!
43
43
 
44
+ You can also create files per environment, such as `.env.test`:
45
+
46
+ S3_BUCKET=test
47
+ SECRET_KEY=test
48
+
44
49
  Whenever your application loads, these variables will be available in `ENV`:
45
50
 
46
51
  config.fog_directory = ENV['S3_BUCKET']
data/dotenv.gemspec CHANGED
@@ -1,6 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |gem|
4
+ gem.version = '0.2.0'
4
5
  gem.authors = ["Brandon Keepers"]
5
6
  gem.email = ["brandon@opensoul.org"]
6
7
  gem.description = %q{Loads environment variables from `.env`.}
@@ -12,7 +13,6 @@ Gem::Specification.new do |gem|
12
13
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
14
  gem.name = "dotenv"
14
15
  gem.require_paths = ["lib"]
15
- gem.version = '0.1.0'
16
16
 
17
17
  gem.add_development_dependency 'rake'
18
18
  gem.add_development_dependency 'rspec'
@@ -2,7 +2,7 @@ module Dotenv
2
2
  class Environment < Hash
3
3
  def initialize(filename)
4
4
  @filename = filename
5
- load
5
+ load if File.exists? @filename
6
6
  end
7
7
 
8
8
  def load
@@ -5,7 +5,7 @@ module Dotenv
5
5
  end
6
6
 
7
7
  initializer 'dotenv', :group => :all do
8
- Dotenv.load
8
+ Dotenv.load '.env', ".env.#{Rails.env}"
9
9
  end
10
10
  end
11
11
  end
data/lib/dotenv.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require 'dotenv/environment'
2
2
 
3
3
  module Dotenv
4
- def self.load(filename = '.env')
5
- Dotenv::Environment.new(filename).apply
4
+ def self.load(*filenames)
5
+ filenames << '.env' if filenames.empty?
6
+ filenames.inject({}) do |hash, filename|
7
+ hash.merge Dotenv::Environment.new(filename).apply
8
+ end
6
9
  end
7
10
  end
8
11
 
9
- require 'dotenv/railtie' if defined?(Rails)
12
+ require 'dotenv/railtie' if defined?(Rails) and defined?(Rails::Railtie)
data/spec/dotenv_spec.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Dotenv::Environment do
3
+ describe Dotenv do
4
4
  let(:env_path) { fixture_path('plain.env') }
5
- let(:dotenv) { Dotenv::Environment.new(env_path) }
6
5
 
7
6
  before do
8
7
  @env_keys = ENV.keys
@@ -12,18 +11,74 @@ describe Dotenv::Environment do
12
11
  ENV.delete_if { |k,v| !@env_keys.include?(k) }
13
12
  end
14
13
 
15
- describe 'initialize' do
16
- it 'reads environment config' do
17
- expect(dotenv['OPTION_A']).to eq('1')
18
- expect(dotenv['OPTION_B']).to eq('2')
14
+ describe 'load' do
15
+ context 'with no args' do
16
+ it 'defaults to .env' do
17
+ Dotenv::Environment.should_receive(:new).with('.env').
18
+ and_return(mock(:apply => {}))
19
+ Dotenv.load
20
+ end
21
+ end
22
+
23
+ context 'with multiple files' do
24
+ let(:expected) do
25
+ {'OPTION_A' => '1', 'OPTION_B' => '2', 'DOTENV' => 'true'}
26
+ end
27
+
28
+ subject do
29
+ Dotenv.load('.env', env_path)
30
+ end
31
+
32
+ it 'loads all files' do
33
+ subject
34
+ expected.each do |key, value|
35
+ expect(ENV[key]).to eq(value)
36
+ end
37
+ end
38
+
39
+ it 'returns hash of loaded environments' do
40
+ expect(subject).to eq(expected)
41
+ end
19
42
  end
20
43
  end
21
44
 
22
- describe 'apply' do
23
- it 'sets variables in ENV' do
24
- dotenv.apply
25
- expect(ENV['OPTION_A']).to eq('1')
45
+ describe Dotenv::Environment do
46
+ subject { Dotenv::Environment.new(env_path) }
47
+
48
+ context 'with a plain env file' do
49
+
50
+ describe 'initialize' do
51
+ it 'reads environment config' do
52
+ expect(subject['OPTION_A']).to eq('1')
53
+ expect(subject['OPTION_B']).to eq('2')
54
+ end
55
+ end
56
+
57
+ describe 'apply' do
58
+ it 'sets variables in ENV' do
59
+ subject.apply
60
+ expect(ENV['OPTION_A']).to eq('1')
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'when the file does not exist' do
66
+ let(:env_path) { fixture_path('.env_does_not_exist') }
67
+
68
+ describe 'initialize' do
69
+ it 'fails silently' do
70
+ expect { Dotenv::Environment.new('.env_does_not_exist') }.not_to raise_error
71
+ end
72
+ end
73
+
74
+ describe 'apply' do
75
+ it 'does not effect env' do
76
+ subject.apply
77
+ expect(ENV.keys).to eq(@env_keys)
78
+ end
79
+ end
26
80
  end
81
+
27
82
  end
28
83
 
29
84
  def fixture_path(name)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brandon Keepers
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-24 00:00:00 Z
18
+ date: 2012-09-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -55,6 +55,7 @@ extensions: []
55
55
  extra_rdoc_files: []
56
56
 
57
57
  files:
58
+ - .env
58
59
  - .gitignore
59
60
  - Gemfile
60
61
  - LICENSE