env_bang 0.1 → 0.2
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.
- checksums.yaml +4 -4
- data/README.md +20 -20
- data/env_bang.gemspec +2 -2
- data/lib/env_bang/version.rb +1 -1
- data/lib/env_bang.rb +37 -11
- data/test/env_bang_test.rb +59 -0
- data/test/test_helper.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f61fe363d185a95ba2baba7dfe930515d5b484ae
|
4
|
+
data.tar.gz: a89ea79b5655e95d808c5c18bd362e2c671010a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fe82b08b5a0646f74a1507d5c086be3782f0efb46d289263fc81eca1427dc0760029114e973188d6b03774a1065bbabc1da14132dbe77ca0f41ca8721702df6
|
7
|
+
data.tar.gz: 9c61fb918a554319585259bc50d09537b89d0dbd5457209fdad34042bde68a34709da0bb2755ed7b781d5e6b1d248d4904e5c49f3154bb33714281bb6902d0bf
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[](https://rubygems.org/gems/env_bang)
|
1
2
|
[](https://travis-ci.org/jcamenisch/ENV_BANG)
|
2
3
|
[](https://gemnasium.com/jcamenisch/ENV_BANG)
|
3
4
|
[](https://codeclimate.com/github/jcamenisch/ENV_BANG)
|
@@ -9,13 +10,13 @@ Do a bang-up job managing your environment variables.
|
|
9
10
|
|
10
11
|
ENV! provides a thin wrapper around ENV to accomplish a few things:
|
11
12
|
|
12
|
-
- Provide a central place to specify
|
13
|
-
- Fail loudly
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
- Provide a central place to specify all your app’s environment variables.
|
14
|
+
- Fail loudly and helpfully if environment variables are missing.
|
15
|
+
- Prevent an application from starting up with missing environment variables.
|
16
|
+
(This is especially helpful in environments like Heroku, as your app will
|
17
|
+
continue running the old code until the server is configured for a new revision.
|
18
|
+
You discover the missing variable immediately instead of your customers
|
19
|
+
discovering it for you later.)
|
19
20
|
|
20
21
|
## Installation
|
21
22
|
|
@@ -51,22 +52,21 @@ end
|
|
51
52
|
A single variable can also be configured with `ENV!.use MY_VAR`, but the `ENV!.config` block
|
52
53
|
will typically contain all the variables for your app.
|
53
54
|
|
54
|
-
Once a variable is specified with `ENV!.use`,
|
55
|
+
Once a variable is specified with `ENV!.use`, access it with
|
55
56
|
|
56
57
|
```ruby
|
57
58
|
ENV!['MY_VAR']
|
58
59
|
```
|
59
60
|
|
60
|
-
This will function just like accessing
|
61
|
-
|
62
|
-
|
63
|
-
what needs to be configured.
|
61
|
+
This will function just like accessing `ENV` directly, except that it will require the variable
|
62
|
+
to have been specified, and be present in the current environment. If either of these conditions
|
63
|
+
is not met, a KeyError will be raised with an explanation of what needs to be configured.
|
64
64
|
|
65
65
|
### Adding a default value
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
For some variables, you’ll want to include a default value in your code, and allow each
|
68
|
+
environment to specify the variable only if the default needs to be overriden. You can
|
69
|
+
accomplish this with the `:default` option:
|
70
70
|
|
71
71
|
```ruby
|
72
72
|
ENV!.config do
|
@@ -78,10 +78,10 @@ end
|
|
78
78
|
|
79
79
|
### Adding a description
|
80
80
|
|
81
|
-
When a new team
|
81
|
+
When a new team member installs or deploys your project, they may run into a missing
|
82
82
|
environment variable error. It can save them a great deal of time to include documentation
|
83
|
-
|
84
|
-
|
83
|
+
along with the error that is raised. To accomplish this, provide a description (of any
|
84
|
+
length) to the `use` method:
|
85
85
|
|
86
86
|
```ruby
|
87
87
|
ENV!.config do
|
@@ -90,8 +90,8 @@ ENV!.config do
|
|
90
90
|
end
|
91
91
|
```
|
92
92
|
|
93
|
-
|
94
|
-
|
93
|
+
Now if someone installs or deploys the app without setting the RAILS_SECRET_KEY_BASE variable,
|
94
|
+
they will see these instructions immediately upon running the app.
|
95
95
|
|
96
96
|
## Contributing
|
97
97
|
|
data/env_bang.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jonathan Camenisch"]
|
10
10
|
spec.email = ["jonathan@camenisch.net"]
|
11
11
|
spec.summary = %q{Do a bang-up job managing your environment variables}
|
12
|
-
spec.description = %q{}
|
13
|
-
spec.homepage = ""
|
12
|
+
spec.description = %q{ENV! provides a thin wrapper around ENV to encourage central, self-documenting configuration and helpful error message.}
|
13
|
+
spec.homepage = "https://github.com/jcamenisch/ENV_BANG"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/env_bang/version.rb
CHANGED
data/lib/env_bang.rb
CHANGED
@@ -11,31 +11,57 @@ class ENV_BANG
|
|
11
11
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
12
12
|
|
13
13
|
unless ENV.has_key?(var)
|
14
|
-
ENV[var] = options.fetch(:default)
|
15
|
-
message = "Missing required environment variable: #{var}"
|
16
|
-
message << "--#{description}" if description
|
17
|
-
indent = ' '
|
18
|
-
raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
|
19
|
-
end
|
14
|
+
ENV[var] = options.fetch(:default) { formatted_error(var, description) }.to_s
|
20
15
|
end
|
21
16
|
|
22
|
-
|
17
|
+
# Store the variable, converted to requested class
|
18
|
+
klass = :"#{options.fetch(:class, String)}"
|
19
|
+
vars[var] = Classes.cast ENV[var], klass, options
|
23
20
|
end
|
24
21
|
|
25
|
-
def
|
26
|
-
|
22
|
+
def formatted_error(var, description)
|
23
|
+
message = "Missing required environment variable: #{var}"
|
24
|
+
message << "\n#{description}" if description
|
25
|
+
indent = ' '
|
26
|
+
raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
|
27
|
+
end
|
28
|
+
|
29
|
+
def vars
|
30
|
+
@vars ||= {}
|
27
31
|
end
|
28
32
|
|
29
33
|
def [](var)
|
30
|
-
raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless
|
34
|
+
raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless vars.has_key?(var)
|
31
35
|
|
32
|
-
|
36
|
+
vars[var]
|
33
37
|
end
|
34
38
|
|
35
39
|
def method_missing(method, *args, &block)
|
36
40
|
ENV.send(method, *args, &block)
|
37
41
|
end
|
38
42
|
end
|
43
|
+
|
44
|
+
module Classes
|
45
|
+
class << self
|
46
|
+
def cast(value, klass, options = {})
|
47
|
+
public_send(:"#{klass}", value, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def boolean(value, options)
|
51
|
+
!(value =~ /^(|0|disabled?|false|no|off)$/i)
|
52
|
+
end
|
53
|
+
|
54
|
+
def Array(value, options)
|
55
|
+
klass = options.fetch(:of, String)
|
56
|
+
value.split(',').map { |value| cast(value.strip, klass) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
|
60
|
+
def method_missing(klass, value, options, &block)
|
61
|
+
Kernel.send(klass, value)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
39
65
|
end
|
40
66
|
|
41
67
|
def ENV!
|
data/test/env_bang_test.rb
CHANGED
@@ -44,4 +44,63 @@ describe ENV_BANG do
|
|
44
44
|
end
|
45
45
|
ENV!['PRESENT'].must_equal 'present in environment'
|
46
46
|
end
|
47
|
+
|
48
|
+
describe "Type casting" do
|
49
|
+
let(:truthy_values) { %w[true on yes yo yup anything] }
|
50
|
+
let(:falsey_values) { %w[false no off disable disabled 0] << '' }
|
51
|
+
let(:integers) { %w[0 1 10 -42 -55] }
|
52
|
+
let(:floats) { %w[0.1 1.3 10 -42.3 -55] }
|
53
|
+
|
54
|
+
it "Casts Integers" do
|
55
|
+
integer = integers.sample
|
56
|
+
ENV['INTEGER'] = integer
|
57
|
+
ENV!.use 'INTEGER', class: Integer
|
58
|
+
|
59
|
+
ENV!['INTEGER'].must_equal integer.to_i
|
60
|
+
end
|
61
|
+
|
62
|
+
it "Casts Floats" do
|
63
|
+
float = floats.sample
|
64
|
+
ENV['FLOAT'] = float
|
65
|
+
ENV!.use 'FLOAT', class: Float
|
66
|
+
|
67
|
+
ENV!['FLOAT'].must_equal float.to_f
|
68
|
+
ENV!['FLOAT'].class.must_equal Float
|
69
|
+
end
|
70
|
+
|
71
|
+
it "Casts Arrays" do
|
72
|
+
ENV['ARRAY'] = 'one,two , three, four'
|
73
|
+
ENV!.use 'ARRAY', class: Array
|
74
|
+
|
75
|
+
ENV!['ARRAY'].must_equal %w[one two three four]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "Casts Arrays of Integers" do
|
79
|
+
ENV['INTEGERS'] = integers.join(',')
|
80
|
+
ENV!.use 'INTEGERS', class: Array, of: Integer
|
81
|
+
|
82
|
+
ENV!['INTEGERS'].must_equal integers.map(&:to_i)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "Casts Arrays of Floats" do
|
86
|
+
ENV['FLOATS'] = floats.join(',')
|
87
|
+
ENV!.use 'FLOATS', class: Array, of: Float
|
88
|
+
|
89
|
+
ENV!['FLOATS'].must_equal floats.map(&:to_f)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "Casts false" do
|
93
|
+
ENV['TRUE'] = truthy_values.sample
|
94
|
+
ENV!.use 'TRUE', class: :boolean
|
95
|
+
|
96
|
+
ENV!['TRUE'].must_equal true
|
97
|
+
end
|
98
|
+
|
99
|
+
it "Casts false" do
|
100
|
+
ENV['FALSE'] = falsey_values.sample
|
101
|
+
ENV!.use 'FALSE', class: :boolean
|
102
|
+
|
103
|
+
ENV!['FALSE'].must_equal false
|
104
|
+
end
|
105
|
+
end
|
47
106
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: env_bang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Camenisch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,7 +38,8 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '5.1'
|
41
|
-
description:
|
41
|
+
description: ENV! provides a thin wrapper around ENV to encourage central, self-documenting
|
42
|
+
configuration and helpful error message.
|
42
43
|
email:
|
43
44
|
- jonathan@camenisch.net
|
44
45
|
executables: []
|
@@ -56,7 +57,7 @@ files:
|
|
56
57
|
- lib/env_bang/version.rb
|
57
58
|
- test/env_bang_test.rb
|
58
59
|
- test/test_helper.rb
|
59
|
-
homepage:
|
60
|
+
homepage: https://github.com/jcamenisch/ENV_BANG
|
60
61
|
licenses:
|
61
62
|
- MIT
|
62
63
|
metadata: {}
|