get_env 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/get_env/version.rb +1 -1
- data/lib/get_env.rb +37 -19
- data/spec/get_env_spec.rb +9 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a69954781ff0a92ff24cbc9200e0605104f7df6228b0f389ea1400af81bbf926
|
4
|
+
data.tar.gz: 1b316b87f08984ced9e07c55bf3bf94f13b9a6b68cc7441a4cf05af77f6cee4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 398a9044aa2d6982f27eaf7a3bd56d7bdb4c690fd5a0d79a166b54ecb4775fc6520a1f5133bc106e440f17162bb148157672c11c55eb5ba0a0a7b02e6b8d2d02
|
7
|
+
data.tar.gz: '036692057e61f8c0e600bcf296e8b161a0122650d4ed02fb0cb049ee805f0e053f6b0a0f98ca50a99994e86a49abf2d0f54b6a66a264650585b561d0824082c9'
|
data/lib/get_env/version.rb
CHANGED
data/lib/get_env.rb
CHANGED
@@ -3,30 +3,48 @@
|
|
3
3
|
require 'get_env/version'
|
4
4
|
|
5
5
|
module GetEnv
|
6
|
-
|
7
|
-
|
6
|
+
class << self
|
7
|
+
def [](key)
|
8
|
+
return nil if key.nil?
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
v = ENV[key].to_i
|
11
|
+
return v if v.to_s == ENV[key]
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
v = ENV[key].to_f
|
14
|
+
return v if v.to_s == ENV[key]
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
return false if ENV[key] == 'false'
|
17
|
+
return true if ENV[key] == 'true'
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
ENV[key]
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch(*args, &block)
|
23
|
+
case args
|
24
|
+
in [key, default] then fetch_with_default(key, default)
|
25
|
+
in [key] then fetch_without_default(key, &block)
|
26
|
+
else raise ArgumentError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def fetch_with_default(key, default)
|
33
|
+
if ENV.has_key?(key)
|
34
|
+
self[key]
|
35
|
+
else
|
36
|
+
default
|
37
|
+
end
|
38
|
+
end
|
20
39
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
ENV.fetch(key) # Will raise a KeyError
|
40
|
+
def fetch_without_default(key, &block)
|
41
|
+
if ENV.has_key?(key)
|
42
|
+
self[key]
|
43
|
+
elsif block_given?
|
44
|
+
block.call
|
45
|
+
else
|
46
|
+
ENV.fetch(key) # Will raise a KeyError
|
47
|
+
end
|
30
48
|
end
|
31
49
|
end
|
32
50
|
end
|
data/spec/get_env_spec.rb
CHANGED
@@ -53,6 +53,10 @@ RSpec.describe GetEnv do
|
|
53
53
|
expect(GetEnv.fetch('SOME_NON_EXISTANT_FLOAT', 3.21)).to eq(3.21)
|
54
54
|
end
|
55
55
|
|
56
|
+
it 'allows nil as a default value' do
|
57
|
+
expect(GetEnv.fetch('SOME_NON_EXISTANT_FLOAT', nil)).to be_nil
|
58
|
+
end
|
59
|
+
|
56
60
|
it 'raises an exception if not found and no default is specified' do
|
57
61
|
expect do
|
58
62
|
GetEnv.fetch('SOME_NON_EXISTANT_FLOAT')
|
@@ -63,5 +67,10 @@ RSpec.describe GetEnv do
|
|
63
67
|
v = GetEnv.fetch('SOME_NON_EXISTANT_FLOAT') { 3.21 }
|
64
68
|
expect(v).to eq(3.21)
|
65
69
|
end
|
70
|
+
|
71
|
+
it 'raises when given too few or too many arguments' do
|
72
|
+
expect { GetEnv.fetch }.to raise_error(ArgumentError)
|
73
|
+
expect { GetEnv.fetch('FOO', 1, 2) }.to raise_error(ArgumentError)
|
74
|
+
end
|
66
75
|
end
|
67
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Mathieu
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2023-10-19 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -118,11 +118,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
- !ruby/object:Gem::Version
|
119
119
|
version: '0'
|
120
120
|
requirements: []
|
121
|
-
rubygems_version: 3.
|
121
|
+
rubygems_version: 3.4.19
|
122
122
|
signing_key:
|
123
123
|
specification_version: 4
|
124
124
|
summary: Read values from ENV in a reasonable way
|
125
125
|
test_files:
|
126
|
-
- spec/spec_helper.rb
|
127
126
|
- spec/get_env_spec.rb
|
128
127
|
- spec/rubocop/cop/lint/no_env_spec.rb
|
128
|
+
- spec/spec_helper.rb
|