env-conf 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c00969bbe2528704fd3247dcf352c9daf44acd0
4
- data.tar.gz: 588806c188226ce34693aaf2bf629687b023cdf6
3
+ metadata.gz: 58250c277c52d0684b972ee12a313a9eab33616e
4
+ data.tar.gz: 1ca6d546f1b641923f7bb668ab54f151c13c2063
5
5
  SHA512:
6
- metadata.gz: 10c18faff50a64c766d84c56f9bdfe37d757b20c42106606683edd775efbf9381a164aaacfea7490ea64e638920780f3f2f5e6d8529987df626f3776d6147287
7
- data.tar.gz: ce4c1e12d0b7b4134ff83df500c386417e2ec55ed099386cc3630f3c707d2917c35ee254bfa9a86ba4eaac75fea81123e5ca2a9bd6af40aa22c66ed43fd46eea
6
+ metadata.gz: 401a87ec96c25822767ef11af6b9133c782d2533b8bd244d983b17d611fe6941f83c4bb221462a348a094581f9d96d9bf4d43399f6e7cef838a26f4d4dc370b3
7
+ data.tar.gz: 673bccc57ac6aa66b0fdb113c4682fbae897e469d89148a63cdc3054d4ff215db07ad531eb6e63bd187615f8014177baae7f4f83d2cc11678e9c8bb6561b4648
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in env-conf.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem "fakefs"
8
+ end
data/env-conf.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "dotenv", "~> 1.0"
21
22
  spec.add_development_dependency "bundler", "~> 1.7"
22
23
  spec.add_development_dependency "rake", "~> 10.0"
23
24
  spec.add_development_dependency "minitest"
data/lib/env-conf.rb CHANGED
@@ -2,6 +2,7 @@ require_relative './env-conf/version'
2
2
 
3
3
  module Config
4
4
  @@defaults = {}
5
+ @@dotenv = {}
5
6
 
6
7
  # Get a Config value
7
8
  #
@@ -28,12 +29,13 @@ module Config
28
29
  def self.[](name)
29
30
  var_name = name.to_s.upcase
30
31
  default_name = name.to_s.downcase.to_sym
31
- ENV[var_name] || @@defaults[default_name]
32
+ ENV[var_name] || @@dotenv[var_name] || @@defaults[default_name]
32
33
  end
33
34
 
34
35
  # Reset defaults values
35
36
  def self.reset!
36
37
  @@defaults = {}
38
+ @@dotenv = {}
37
39
  end
38
40
 
39
41
  # An environment variable.
@@ -178,4 +180,15 @@ module Config
178
180
  def self.uri(name)
179
181
  self[name] && URI.parse(self[name])
180
182
  end
183
+
184
+ # Loads a ".env" file, using Dotenv to parse but not fuck up the ENV
185
+ def self.dotenv!
186
+ return if Config.production?
187
+ require 'dotenv'
188
+ ['.env','.env.local',".env.#{Config[:rack_env]}"].each do |filename|
189
+ if File.exists?(filename)
190
+ @@dotenv.update(Dotenv::Parser.call(File.read(filename)))
191
+ end
192
+ end
193
+ end
181
194
  end
@@ -2,5 +2,5 @@
2
2
  Object.send(:remove_const, :Config) if defined?(Config)
3
3
 
4
4
  module Config
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
  end
data/test/config_test.rb CHANGED
@@ -1,40 +1,6 @@
1
- # note: this would all go in a test/helper if we had
2
- # more than one test file
3
- require 'minitest'
4
- require 'minitest/autorun'
5
- require 'minitest/pride'
6
-
7
- require 'env-conf'
8
-
9
- module EnvironmentHelpers
10
- # Override an environment variable in the current test.
11
- def set_env(key, value)
12
- @overrides[key] = ENV[key] unless @overrides.has_key?(key)
13
- ENV[key] = value
14
- end
15
-
16
- def setup
17
- super
18
- @overrides = {}
19
- end
20
-
21
- # Restore the environment back to its state before tests ran.
22
- def teardown
23
- @overrides.each { |key, value| ENV[key] = value }
24
- super
25
- end
26
- end
27
-
28
- # end note
29
-
30
- class ConfigTest < Minitest::Test
31
- include EnvironmentHelpers
32
-
33
- def teardown
34
- super
35
- Config.reset!
36
- end
1
+ require 'helper'
37
2
 
3
+ class ConfigTest < Test
38
4
  # Config.env returns the value matching the specified environment
39
5
  # variable name.
40
6
  def test_env
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class DotenvTest < Test
4
+ def setup
5
+ super
6
+ FakeFS.activate!
7
+ end
8
+
9
+ def teardown
10
+ super
11
+ %w{.env .env.local .env.test}.each do |f|
12
+ File.unlink(f) if File.exists?(f)
13
+ end
14
+ FakeFS.deactivate!
15
+ end
16
+
17
+ def test_one_var
18
+ File.open(".env",'w') { |f| f << "FOO=bar" }
19
+ assert_equal(nil, Config[:foo])
20
+ Config.dotenv!
21
+ assert_equal('bar', Config[:foo])
22
+ end
23
+
24
+ def test_two_files
25
+ File.open(".env",'w') { |f| f << "FOO=bar" }
26
+ File.open(".env.local",'w') { |f| f << "FOO=zzz" }
27
+ assert_equal(nil, Config[:foo])
28
+ Config.dotenv!
29
+ assert_equal('zzz', Config[:foo])
30
+ end
31
+
32
+ def test_env_specific
33
+ File.open(".env",'w') { |f| f << "FOO=bar" }
34
+ File.open(".env.local",'w') { |f| f << "FOO=zzz" }
35
+ File.open(".env.test",'w') { |f| f << "FOO=test" }
36
+ assert_equal(nil, Config[:foo])
37
+ set_env('RACK_ENV', 'test')
38
+ Config.dotenv!
39
+ assert_equal('test', Config[:foo])
40
+ end
41
+
42
+ def test_noop_in_prod
43
+ File.open(".env",'w') { |f| f << "FOO=bar" }
44
+ File.open(".env.local",'w') { |f| f << "FOO=zzz" }
45
+ File.open(".env.test",'w') { |f| f << "FOO=test" }
46
+ assert_equal(nil, Config[:foo])
47
+ set_env('RACK_ENV', 'production')
48
+ Config.dotenv!
49
+ assert_equal(nil, Config[:foo])
50
+ end
51
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+ require 'fakefs/safe'
5
+
6
+ require 'env-conf'
7
+
8
+ module EnvironmentHelpers
9
+ # Override an environment variable in the current test.
10
+ def set_env(key, value)
11
+ @overrides[key] = ENV[key] unless @overrides.has_key?(key)
12
+ ENV[key] = value
13
+ end
14
+
15
+ def setup
16
+ super
17
+ @overrides = {}
18
+ end
19
+
20
+ # Restore the environment back to its state before tests ran.
21
+ def teardown
22
+ @overrides.each { |key, value| ENV[key] = value }
23
+ super
24
+ end
25
+ end
26
+
27
+ class Test < Minitest::Test
28
+ include EnvironmentHelpers
29
+
30
+ def teardown
31
+ super
32
+ Config.reset!
33
+ end
34
+ end
35
+
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env-conf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Continanza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +82,8 @@ files:
68
82
  - lib/env-conf.rb
69
83
  - lib/env-conf/version.rb
70
84
  - test/config_test.rb
85
+ - test/dotenv_test.rb
86
+ - test/helper.rb
71
87
  homepage: ''
72
88
  licenses:
73
89
  - MIT
@@ -88,9 +104,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
104
  version: '0'
89
105
  requirements: []
90
106
  rubyforge_project:
91
- rubygems_version: 2.2.2
107
+ rubygems_version: 2.2.0
92
108
  signing_key:
93
109
  specification_version: 4
94
110
  summary: Use ENV vars for Configuration
95
111
  test_files:
96
112
  - test/config_test.rb
113
+ - test/dotenv_test.rb
114
+ - test/helper.rb