environs 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/environs/env.rb +22 -0
- data/lib/environs/missing_env_var_error.rb +3 -0
- data/lib/environs/version.rb +1 -1
- data/lib/environs.rb +0 -4
- data/spec/environs/env_spec.rb +28 -0
- metadata +6 -2
data/lib/environs/env.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'environs/missing_env_var_error'
|
2
|
+
|
3
|
+
class Env
|
4
|
+
class << self
|
5
|
+
def method_missing(method, *args, &block)
|
6
|
+
key = method.to_s.upcase
|
7
|
+
if key.match(/\w+/)
|
8
|
+
ENV[key] || key_not_found_response(args.first, key)
|
9
|
+
else
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def key_not_found_response(potential_hash, key)
|
15
|
+
if potential_hash.respond_to?(:fetch) && potential_hash.fetch(:allow_nil)
|
16
|
+
nil
|
17
|
+
else
|
18
|
+
raise MissingEnvVarError, "The #{key} environment variable is not set."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/environs/version.rb
CHANGED
data/lib/environs.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'environs'
|
2
|
+
|
3
|
+
describe Env, "#foo" do
|
4
|
+
|
5
|
+
it "returns the ENV variable corresponding to the name of the method" do
|
6
|
+
ENV['FOO'] = 'bar'
|
7
|
+
Env.foo.should eq 'bar'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "default behavior" do
|
11
|
+
|
12
|
+
it "returns an error if no corresponding ENV variable is set" do
|
13
|
+
ENV['FOO'] = nil
|
14
|
+
expect { Env.foo }.to raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "'allow_nil: true' is passed along" do
|
20
|
+
|
21
|
+
it "returns nil if no corresponding ENV var is set" do
|
22
|
+
ENV['FOO'] = nil
|
23
|
+
Env.foo(allow_nil: true).should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: environs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Colin Rymer
|
@@ -74,7 +74,10 @@ files:
|
|
74
74
|
- Rakefile
|
75
75
|
- environs.gemspec
|
76
76
|
- lib/environs.rb
|
77
|
+
- lib/environs/env.rb
|
78
|
+
- lib/environs/missing_env_var_error.rb
|
77
79
|
- lib/environs/version.rb
|
80
|
+
- spec/environs/env_spec.rb
|
78
81
|
homepage: ''
|
79
82
|
licenses:
|
80
83
|
- MIT
|
@@ -101,5 +104,6 @@ signing_key:
|
|
101
104
|
specification_version: 3
|
102
105
|
summary: A simple wrapper around the ENV hash that by default raises an error when
|
103
106
|
value requested is not set.
|
104
|
-
test_files:
|
107
|
+
test_files:
|
108
|
+
- spec/environs/env_spec.rb
|
105
109
|
has_rdoc:
|