envar 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +88 -0
- data/Rakefile +1 -0
- data/envar.gemspec +27 -0
- data/lib/envar.rb +6 -0
- data/lib/envar/accessor.rb +13 -0
- data/lib/envar/config.rb +41 -0
- data/lib/envar/version.rb +3 -0
- data/spec/accessor_spec.rb +20 -0
- data/spec/config_spec.rb +82 -0
- data/spec/spec_helper.rb +21 -0
- metadata +145 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kane Baccigalupi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Envar
|
2
|
+
|
3
|
+
Envar is a small shim between the ENV variables which are a pain to
|
4
|
+
mock, and your app which needs to act differently according to
|
5
|
+
environments. Under the covers Envar uses the Dotenv gem, which loads
|
6
|
+
environmental variables from a .env file in your development
|
7
|
+
environment. Envar does not so much, but it does it in a way that makes
|
8
|
+
your life much easier!
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'envar'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install envar
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### With Rails:
|
27
|
+
|
28
|
+
Envar was built to just work with Rails. If you are running in a
|
29
|
+
development or a test environment, Envar will auto-load the .env file.
|
30
|
+
Those variables will be available on the top level constant:
|
31
|
+
|
32
|
+
With the `Rails.env = 'development'` and a .env file like so:
|
33
|
+
|
34
|
+
FACEBOOK_TOKEN=fb-for-life-YO
|
35
|
+
|
36
|
+
Variables from the .env file will be loaded into the ENV variables
|
37
|
+
space. Even better, the top level Envar space provides a helpful shim
|
38
|
+
for accessing these variables so that you don't have to get all angle
|
39
|
+
brackety.
|
40
|
+
|
41
|
+
Envar.facebook_token
|
42
|
+
# => fb-for-life-YO
|
43
|
+
|
44
|
+
# or the uglier uppercase version, if you prefer
|
45
|
+
Envar.FACEBOOK_TOKEN
|
46
|
+
# => fb-for-life-YO
|
47
|
+
|
48
|
+
This is a lot easier to work with in tests. For example, in rspec it
|
49
|
+
looks like this:
|
50
|
+
|
51
|
+
Envar.stub(:facebook_token).and_return('my_great_token')
|
52
|
+
|
53
|
+
In ordinary circumstances you would need to set the ENV variables or
|
54
|
+
assume in test knowlege about the .env variable. Or worse, stub out the
|
55
|
+
:[] angle bracket methods. Try not to barf a little in your mouth if you
|
56
|
+
are in that ugly situation. Instead, be happy with Envar.
|
57
|
+
|
58
|
+
For extra configuration stuff, checkout the section below that details
|
59
|
+
what to do without some Rails.
|
60
|
+
|
61
|
+
### Without Rails:
|
62
|
+
|
63
|
+
Set the Envar environment by hand:
|
64
|
+
|
65
|
+
Envar::Config.environment = 'development' # or whatever
|
66
|
+
|
67
|
+
The development and test environment will trigger the loading of the
|
68
|
+
local .env file. In other environments it is expected that you will have
|
69
|
+
real environmental variables set.
|
70
|
+
|
71
|
+
If you find you would rather set .env files in other environments, you
|
72
|
+
can configure which environments should load the .env file:
|
73
|
+
|
74
|
+
Envar::Config.load_environments = ['development', 'staging']
|
75
|
+
# => ['development', 'staging']
|
76
|
+
|
77
|
+
# or to modify the defaults which are ['development', 'test']
|
78
|
+
|
79
|
+
Envar::Config.load_environments << ['staging']
|
80
|
+
# => ['development', 'test', 'staging']
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
87
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
88
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/envar.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'envar/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "envar"
|
8
|
+
spec.version = Envar::VERSION
|
9
|
+
spec.authors = ["Kane Baccigalupi"]
|
10
|
+
spec.email = ["baccigalupi@gmail.com"]
|
11
|
+
spec.description = %q{Envar is a simple wrapper around your environmental variables to ease development and testing}
|
12
|
+
spec.summary = %q{Envar is a simple wrapper around your environmental variables to ease development and testing}
|
13
|
+
spec.homepage = "http://github.com/baccigalupi/envar"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'dotenv'
|
22
|
+
spec.add_dependency 'active_support'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
end
|
data/lib/envar.rb
ADDED
data/lib/envar/config.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Envar
|
2
|
+
class Config
|
3
|
+
class << self
|
4
|
+
delegate :load_environments, :load_environments=,
|
5
|
+
:environment, :environment=,
|
6
|
+
:load?, :load, :loaded?,
|
7
|
+
to: :config
|
8
|
+
|
9
|
+
def config
|
10
|
+
@config ||= Envar::Config.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def reset
|
14
|
+
@config = nil
|
15
|
+
config
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :environment
|
20
|
+
attr_writer :load_environments
|
21
|
+
|
22
|
+
def load_environments
|
23
|
+
@load_environments ||= ['development', 'test']
|
24
|
+
end
|
25
|
+
|
26
|
+
def load?
|
27
|
+
load_environments.include?(environment)
|
28
|
+
end
|
29
|
+
|
30
|
+
def loaded?
|
31
|
+
@loaded
|
32
|
+
end
|
33
|
+
|
34
|
+
def load
|
35
|
+
if load? && !loaded?
|
36
|
+
Dotenv.load
|
37
|
+
@loaded = true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Envar, 'as an enivornment accessor' do
|
4
|
+
before do
|
5
|
+
Envar::Config.environment = 'test' # for autoloading of Dotenv
|
6
|
+
end
|
7
|
+
|
8
|
+
it "loads Dotenv when appropriate" do
|
9
|
+
Envar.facebook_token
|
10
|
+
ENV['FACEBOOK_TOKEN'].should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns the value of the env variable" do
|
14
|
+
Envar.facebook_token.should == 'fb-for-life-YO'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "works with upcase calls too" do
|
18
|
+
Envar.FACEBOOK_TOKEN.should == 'fb-for-life-YO'
|
19
|
+
end
|
20
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Envar::Config do
|
4
|
+
before do
|
5
|
+
Envar::Config.reset
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'allows setting of the current environment' do
|
9
|
+
Envar::Config.environment.should be_nil
|
10
|
+
Envar::Config.environment = 'development'
|
11
|
+
Envar::Config.environment.should == 'development'
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'autoloading environments' do
|
15
|
+
it 'defaults to test and development' do
|
16
|
+
Envar::Config.load_environments.should =~ ['development', 'test']
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'defaults can be modified' do
|
20
|
+
Envar::Config.load_environments << 'staging'
|
21
|
+
Envar::Config.load_environments.should =~ ['development', 'staging', 'test']
|
22
|
+
end
|
23
|
+
|
24
|
+
it "allows complete overriding of load environments" do
|
25
|
+
Envar::Config.load_environments = ['staging']
|
26
|
+
Envar::Config.load_environments.should == ['staging']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.load?' do
|
31
|
+
it "will be true if the environment is in the load environments" do
|
32
|
+
Envar::Config.environment = 'test'
|
33
|
+
Envar::Config.load?.should == true
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'will be false otherwise' do
|
37
|
+
Envar::Config.environment = 'foo'
|
38
|
+
Envar::Config.load?.should == false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.load' do
|
43
|
+
context 'when the current environment is one of the load environments' do
|
44
|
+
before do
|
45
|
+
Envar::Config.environment = 'test'
|
46
|
+
Dotenv.stub(:load)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should load the Dotenv" do
|
50
|
+
Dotenv.should_receive(:load)
|
51
|
+
Envar::Config.load
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should mark itself as loaded" do
|
55
|
+
Envar::Config.load
|
56
|
+
Envar::Config.loaded?.should == true
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'after it has been loaded at least once' do
|
60
|
+
before do
|
61
|
+
Envar::Config.load
|
62
|
+
end
|
63
|
+
|
64
|
+
it "will not try to reload the Dotenv" do
|
65
|
+
Dotenv.should_not_receive(:load)
|
66
|
+
Envar::Config.load
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when the environment should not be autoloaded" do
|
72
|
+
before do
|
73
|
+
Envar::Config.environment = 'foo'
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should not try to load the Dotenv' do
|
77
|
+
Dotenv.should_not_receive(:load)
|
78
|
+
Envar::Config.load
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
$: << File.dirname(__FILE__) + "/../lib"
|
9
|
+
require 'envar'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
# Run specs in random order to surface order dependencies. If you find an
|
17
|
+
# order dependency and want to debug it, you can fix the order by providing
|
18
|
+
# the seed, which is printed after each run.
|
19
|
+
# --seed 1234
|
20
|
+
config.order = 'random'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: envar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kane Baccigalupi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dotenv
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: active_support
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Envar is a simple wrapper around your environmental variables to ease
|
95
|
+
development and testing
|
96
|
+
email:
|
97
|
+
- baccigalupi@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .rspec
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- envar.gemspec
|
109
|
+
- lib/envar.rb
|
110
|
+
- lib/envar/accessor.rb
|
111
|
+
- lib/envar/config.rb
|
112
|
+
- lib/envar/version.rb
|
113
|
+
- spec/accessor_spec.rb
|
114
|
+
- spec/config_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
homepage: http://github.com/baccigalupi/envar
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Envar is a simple wrapper around your environmental variables to ease development
|
141
|
+
and testing
|
142
|
+
test_files:
|
143
|
+
- spec/accessor_spec.rb
|
144
|
+
- spec/config_spec.rb
|
145
|
+
- spec/spec_helper.rb
|