env_bang 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/env_bang/formatter.rb +22 -0
- data/lib/env_bang/version.rb +1 -1
- data/lib/env_bang.rb +4 -6
- data/test/env_bang_test.rb +34 -11
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfdb5e43a5f35d5cea9f39f538f83504d4f77869
|
4
|
+
data.tar.gz: f11fd02adf4720dcec9a4f6161aa618bc259f877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 161419e599bed888d9f6177a3113685379d32710712c6d20a6953e16389d1ef8aaa291d9474a337b187c9e9b640a837cb0802e08827a1912365a802f547a6161
|
7
|
+
data.tar.gz: b90d49748dba09ae7633796290f31a2931dcb7478f58b95854eddfd0e0eeaf7a2b4225bd07678cac4792adeff1e97e79deb6f93c92e4c20a83c1361bd9fab6da
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class ENV_BANG
|
2
|
+
module Formatter
|
3
|
+
class << self
|
4
|
+
def formatted_error(var, description)
|
5
|
+
indent 4, <<-EOS
|
6
|
+
|
7
|
+
Missing required environment variable: #{var}#{ description and "\n" <<
|
8
|
+
unindent(description) }
|
9
|
+
EOS
|
10
|
+
end
|
11
|
+
|
12
|
+
def unindent(string)
|
13
|
+
width = string.scan(/^ */).map(&:length).min
|
14
|
+
string.gsub(/^ {#{width}}/, '')
|
15
|
+
end
|
16
|
+
|
17
|
+
def indent(width, string)
|
18
|
+
string.gsub "\n", "\n#{' ' * width}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/env_bang/version.rb
CHANGED
data/lib/env_bang.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "env_bang/version"
|
2
|
+
require "env_bang/formatter"
|
2
3
|
|
3
4
|
class ENV_BANG
|
4
5
|
class << self
|
@@ -11,17 +12,14 @@ class ENV_BANG
|
|
11
12
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
12
13
|
|
13
14
|
unless ENV.has_key?(var)
|
14
|
-
ENV[var] = options.fetch(:default) {
|
15
|
+
ENV[var] = options.fetch(:default) { raise_formatted_error(var, description) }.to_s
|
15
16
|
end
|
16
17
|
|
17
18
|
vars[var] = options
|
18
19
|
end
|
19
20
|
|
20
|
-
def
|
21
|
-
|
22
|
-
message << "\n#{description}" if description
|
23
|
-
indent = ' '
|
24
|
-
raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
|
21
|
+
def raise_formatted_error(var, description)
|
22
|
+
raise KeyError.new Formatter.formatted_error(var, description)
|
25
23
|
end
|
26
24
|
|
27
25
|
def vars
|
data/test/env_bang_test.rb
CHANGED
@@ -16,17 +16,6 @@ describe ENV_BANG do
|
|
16
16
|
}.must_raise KeyError
|
17
17
|
end
|
18
18
|
|
19
|
-
it "Includes provided description in error message" do
|
20
|
-
ENV.delete('NOT_PRESENT')
|
21
|
-
|
22
|
-
e = proc {
|
23
|
-
ENV!.config do
|
24
|
-
use 'NOT_PRESENT', 'You need a NOT_PRESENT var in your ENV'
|
25
|
-
end
|
26
|
-
}.must_raise(KeyError)
|
27
|
-
e.message.must_include 'You need a NOT_PRESENT var in your ENV'
|
28
|
-
end
|
29
|
-
|
30
19
|
it "Uses provided default value if ENV var not already present" do
|
31
20
|
ENV.delete('WASNT_PRESENT')
|
32
21
|
|
@@ -124,4 +113,38 @@ describe ENV_BANG do
|
|
124
113
|
ENV!['FALSE'].class.must_equal String
|
125
114
|
end
|
126
115
|
end
|
116
|
+
|
117
|
+
describe "Formatting" do
|
118
|
+
it "Includes provided description in error message" do
|
119
|
+
ENV.delete('NOT_PRESENT')
|
120
|
+
|
121
|
+
e = proc {
|
122
|
+
ENV!.config do
|
123
|
+
use 'NOT_PRESENT', 'You need a NOT_PRESENT var in your ENV'
|
124
|
+
end
|
125
|
+
}.must_raise(KeyError)
|
126
|
+
e.message.must_include 'You need a NOT_PRESENT var in your ENV'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "Removes indentation from provided descriptions" do
|
130
|
+
ENV.delete('NOT_PRESENT')
|
131
|
+
|
132
|
+
e = proc {
|
133
|
+
ENV!.config do
|
134
|
+
use 'NOT_PRESENT', <<-DESC
|
135
|
+
This multiline description
|
136
|
+
has a lot of indentation
|
137
|
+
varying from line to line
|
138
|
+
like so
|
139
|
+
DESC
|
140
|
+
end
|
141
|
+
}.must_raise(KeyError)
|
142
|
+
e.message.must_include <<-UNINDENTED
|
143
|
+
This multiline description
|
144
|
+
has a lot of indentation
|
145
|
+
varying from line to line
|
146
|
+
like so
|
147
|
+
UNINDENTED
|
148
|
+
end
|
149
|
+
end
|
127
150
|
end
|
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.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Camenisch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- env_bang.gemspec
|
57
57
|
- lib/env_bang-rails.rb
|
58
58
|
- lib/env_bang.rb
|
59
|
+
- lib/env_bang/formatter.rb
|
59
60
|
- lib/env_bang/version.rb
|
60
61
|
- test/env_bang_test.rb
|
61
62
|
- test/test_helper.rb
|