env.rb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/env.rb CHANGED
@@ -6,6 +6,8 @@ class EnvironmentError < StandardError
6
6
  end
7
7
 
8
8
  module Env
9
+ RACK = %w{GEM_HOME TMPDIR HTTPS}
10
+ HEROKU = %w{TMP TEMP} + RACK
9
11
  @@dependencies = []
10
12
  @@env = {}
11
13
  @@enforced = false
@@ -27,7 +29,11 @@ module Env
27
29
  end
28
30
 
29
31
  def import(key)
30
- export(key, ENV.get(key))
32
+ if key.is_a? Symbol
33
+ const_get(key.to_s.upcase).each { |key| import(key) }
34
+ else
35
+ export(key, ENV.get(key))
36
+ end
31
37
  end
32
38
 
33
39
  def load!
@@ -36,6 +42,20 @@ module Env
36
42
  File.exist?("Envfile")
37
43
  end
38
44
 
45
+ def unload
46
+ @@enforced and Env.unenforce
47
+ @@dependencies = []
48
+ @@env = {}
49
+ end
50
+
51
+ def unenforce
52
+ class << ENV
53
+ alias_method :[], :get
54
+ alias_method :[]=, :set
55
+ end
56
+ @@enforced = false
57
+ end
58
+
39
59
  def enforce
40
60
  class << ENV
41
61
  alias_method :get, :[]
@@ -54,7 +74,7 @@ module Env
54
74
 
55
75
  private
56
76
  def _raise(key)
57
- raise EnvironmentError, "#{key} is not a declared depency, add it to your Envfile"
77
+ raise EnvironmentError, "#{key} is not a declared dependency, add it to your Envfile"
58
78
  end
59
79
 
60
80
  def uri?(value)
data/lib/env/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Env
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/enforce_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative '../lib/env'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Env, "::enforce" do
4
4
  before { Env.enforce }
data/spec/import_spec.rb CHANGED
@@ -1,15 +1,17 @@
1
- require_relative '../lib/env'
1
+ require 'spec_helper'
2
2
 
3
- describe Env, 'import' do
4
- def envfile(string)
5
- File.open("Envfile", 'w') do |f|
6
- f << string
7
- end
3
+ describe Env, '::import' do
4
+ after { File.unlink('Envfile') }
5
+
6
+ let(:rack) do
7
+ %w{GEM_HOME TMPDIR HTTPS}
8
8
  end
9
9
 
10
- after { File.unlink('Envfile') }
10
+ let(:heroku) do
11
+ %w{TMP TEMP} + rack
12
+ end
11
13
 
12
- context "with an import statement" do
14
+ context "import 'FOO'" do
13
15
  before do
14
16
  ENV['FOO'] = 'bar'
15
17
  envfile(%{
@@ -18,8 +20,40 @@ describe Env, 'import' do
18
20
  Env.load!
19
21
  end
20
22
 
21
- it "should use the value in the ENV" do
23
+ it "should use the value in ENV['FOO']" do
22
24
  ENV['FOO'].should == 'bar'
23
25
  end
24
26
  end
27
+
28
+ context "import :rack" do
29
+ before do
30
+ rack.each { |var| ENV[var] = 'foo' }
31
+ envfile(%{
32
+ import :rack
33
+ })
34
+ Env.load!
35
+ end
36
+
37
+ it "should import GEM_HOME, TMPDIR, and HTTPS" do
38
+ rack.each do |var|
39
+ lambda { ENV[var] }.should_not raise_error
40
+ end
41
+ end
42
+ end
43
+
44
+ context "import :heroku" do
45
+ before do
46
+ heroku.each { |var| ENV[var] = 'foo' }
47
+ envfile(%{
48
+ import :heroku
49
+ })
50
+ Env.load!
51
+ end
52
+
53
+ it "should import TMP, TEMP, and RACK" do
54
+ heroku.each do |var|
55
+ lambda { ENV[var] }.should_not raise_error
56
+ end
57
+ end
58
+ end
25
59
  end
data/spec/load_spec.rb CHANGED
@@ -1,11 +1,7 @@
1
- require_relative '../lib/env'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Env, '::load!' do
4
- def envfile(string)
5
- File.open("Envfile", 'w') do |f|
6
- f << string
7
- end
8
- end
4
+ after { Env.unload }
9
5
 
10
6
  context "with no Envfile" do
11
7
  it "should return false" do
@@ -0,0 +1,11 @@
1
+ require_relative '../lib/env'
2
+
3
+ def envfile(string)
4
+ File.open("Envfile", 'w') do |f|
5
+ f << string
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.after(:each) { Env.unload }
11
+ end
data/spec/uri_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative '../lib/env'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Env, 'uri support' do
4
4
  context "with a value FOO that is not a URI" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -32,6 +32,7 @@ files:
32
32
  - spec/enforce_spec.rb
33
33
  - spec/import_spec.rb
34
34
  - spec/load_spec.rb
35
+ - spec/spec_helper.rb
35
36
  - spec/uri_spec.rb
36
37
  has_rdoc: true
37
38
  homepage: ''
@@ -62,4 +63,5 @@ test_files:
62
63
  - spec/enforce_spec.rb
63
64
  - spec/import_spec.rb
64
65
  - spec/load_spec.rb
66
+ - spec/spec_helper.rb
65
67
  - spec/uri_spec.rb