env.rb 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in env.gemspec
4
4
  gemspec
5
+
6
+ gem 'ruby-debug19'
data/lib/env.rb CHANGED
@@ -8,11 +8,17 @@ end
8
8
  module Env
9
9
  RACK = %w{GEM_HOME TMPDIR HTTPS}
10
10
  HEROKU = %w{TMP TEMP} + RACK
11
- @@dependencies = []
12
- @@env = {}
13
- @@enforced = false
11
+ EXPORT_DEFAULTS = {:group => :default}
14
12
 
15
13
  class << self
14
+ def init
15
+ @@dependencies = []
16
+ @@env = {}
17
+ @@enforced = false
18
+ @@groups = Hash.new { |hash, key| hash[key] = [] }
19
+ @@default_group = :default
20
+ end
21
+
16
22
  def [](key)
17
23
  _raise(key) unless dependencies.include? key
18
24
  @@env[key]
@@ -23,11 +29,26 @@ module Env
23
29
  @@env[key] = uri?(value) ? proxify(value) : value
24
30
  end
25
31
 
26
- def export(key, value = nil)
27
- @@dependencies << key
28
- @@env[key] = uri?(value) ? proxify(value) : value
32
+ def group(name)
33
+ @@default_group = name.to_sym
34
+ yield
35
+ @@default_group = :default
36
+ end
37
+
38
+ def export_defaults
39
+ {:group => @@default_group}
40
+ end
41
+
42
+ #store actual work and load group
43
+ def export(key, value = nil, options = {})
44
+ options = export_defaults.merge!(options)
45
+ @@groups[options[:group].to_sym] << lambda do
46
+ @@dependencies << key
47
+ @@env[key] = uri?(value) ? proxify(value) : value
48
+ end
29
49
  end
30
50
 
51
+ #only calls itself and export
31
52
  def import(key)
32
53
  if key.is_a? Symbol
33
54
  const_get(key.to_s.upcase).each { |key| import(key) }
@@ -36,16 +57,23 @@ module Env
36
57
  end
37
58
  end
38
59
 
39
- def load!
60
+ def load_groups(*groups)
61
+ groups.each do |group|
62
+ @@groups[group.to_sym].each { |c| c.call }
63
+ end
64
+ end
65
+
66
+ def load!(*groups)
40
67
  @@enforced or Env.enforce
41
68
  eval File.read("Envfile") if File.exist?("Envfile")
69
+ groups = [:default] if groups.empty?
70
+ load_groups(*groups)
42
71
  File.exist?("Envfile")
43
72
  end
44
73
 
45
74
  def unload
46
75
  @@enforced and Env.unenforce
47
- @@dependencies = []
48
- @@env = {}
76
+ init
49
77
  end
50
78
 
51
79
  def unenforce
@@ -109,3 +137,5 @@ module Env
109
137
  end
110
138
  end
111
139
  end
140
+
141
+ Env.init
data/lib/env/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Env
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/spec/enforce_spec.rb CHANGED
@@ -9,9 +9,10 @@ describe Env, "::enforce" do
9
9
 
10
10
  context "with uninitialized dependency FOO" do
11
11
  before do
12
- Env.instance_eval do
12
+ envfile(%{
13
13
  export 'FOO'
14
- end
14
+ })
15
+ Env.load!
15
16
  end
16
17
 
17
18
  it "should return nil for ENV['FOO']" do
@@ -26,9 +27,10 @@ describe Env, "::enforce" do
26
27
 
27
28
  context "with initialized dependency FOO=bar" do
28
29
  before do
29
- Env.instance_eval do
30
+ envfile(%{
30
31
  export 'FOO', 'bar'
31
- end
32
+ })
33
+ Env.load!
32
34
  end
33
35
 
34
36
  it "should return 'bar' for ENV['FOO']" do
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "handling groups" do
4
+ it "should only be loaded when that group is loaded" do
5
+ Env.load!
6
+ lambda { ENV["URL"] }.should raise_error(EnvironmentError)
7
+ Env.load! :test
8
+ lambda { ENV["URL"] }.should_not raise_error(EnvironmentError)
9
+ ENV['URL'].should == 'http://google.com'
10
+ end
11
+
12
+ it "should allow loading in one command" do
13
+ Env.load! :default, 'test'
14
+ lambda { ENV["URL"] }.should_not raise_error(EnvironmentError)
15
+ ENV['URL'].should == 'http://google.com'
16
+ end
17
+ end
18
+
19
+ describe Env, "groups" do
20
+ context "passed as a parameter" do
21
+ before do
22
+ envfile(%{
23
+ export 'URL', 'http://google.com', :group => :test
24
+ })
25
+ end
26
+
27
+ it_should_behave_like "handling groups"
28
+ end
29
+
30
+
31
+ context "in block form" do
32
+ before do
33
+ envfile(%{
34
+ group :test do
35
+ export 'URL', 'http://google.com'
36
+ end
37
+ })
38
+ end
39
+
40
+ it_should_behave_like "handling groups"
41
+ end
42
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,4 +8,5 @@ end
8
8
 
9
9
  RSpec.configure do |config|
10
10
  config.after(:each) { Env.unload }
11
+ config.after(:each) { File.unlink("Envfile") if File.exist?("Envfile")}
11
12
  end
data/spec/uri_spec.rb CHANGED
@@ -3,10 +3,10 @@ require 'spec_helper'
3
3
  describe Env, 'uri support' do
4
4
  context "with a value FOO that is not a URI" do
5
5
  before do
6
- Env.instance_eval do
6
+ envfile(%{
7
7
  export 'FOO', 'bar'
8
- end
9
- Env.enforce
8
+ })
9
+ Env.load!
10
10
  end
11
11
 
12
12
  it "should not wrap it" do
@@ -18,10 +18,10 @@ describe Env, 'uri support' do
18
18
  URL = 'http://username:password@this.domain.example.com/path?var=val'
19
19
 
20
20
  before do
21
- Env.instance_eval do
21
+ envfile(%{
22
22
  export 'FOO', URL
23
- end
24
- Env.enforce
23
+ })
24
+ Env.load!
25
25
  end
26
26
 
27
27
  it "should leave the original value unchanged" 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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-09 00:00:00.000000000 -07:00
12
+ date: 2011-08-10 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
  description: Allows your to manage many ENV vars by declaring them as dependencies
@@ -30,6 +30,7 @@ files:
30
30
  - lib/env/load.rb
31
31
  - lib/env/version.rb
32
32
  - spec/enforce_spec.rb
33
+ - spec/group_spec.rb
33
34
  - spec/import_spec.rb
34
35
  - spec/load_spec.rb
35
36
  - spec/spec_helper.rb
@@ -61,6 +62,7 @@ specification_version: 3
61
62
  summary: Manage your ENV with ease
62
63
  test_files:
63
64
  - spec/enforce_spec.rb
65
+ - spec/group_spec.rb
64
66
  - spec/import_spec.rb
65
67
  - spec/load_spec.rb
66
68
  - spec/spec_helper.rb