get_env 0.2.0 → 0.2.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/lib/get_env/version.rb +1 -1
- data/lib/get_env.rb +37 -19
- data/lib/rubocop/cop/lint/no_env.rb +4 -6
- data/spec/get_env_spec.rb +9 -0
- data/spec/rubocop/cop/lint/no_env_spec.rb +3 -3
- 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: 573449592055de9a5cf540a80ac3bcf9cf37c30c8a8270d0a354d382dadec9a6
|
4
|
+
data.tar.gz: fe49d463cecf35f961809aa7c6e2ea71cabcf846b36e7d5bde33fd12577e3e20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c1c7974ef31f6b82e9bb6d3acfcb7cf77eed8303662d808740195b800a695f8149a99321c90c6d04836880889b93cb690094b8904d820786597b436747818b3
|
7
|
+
data.tar.gz: 7e4fe1661df9f16f37d2c4184348223852bb7f91fdced7ad729d0eb6fb698f1555d91546b84be4f9b6b44e9597150264ebdf728e03d5fc9db4f76998e7e62fb8
|
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
|
@@ -15,7 +15,9 @@ module RuboCop
|
|
15
15
|
#
|
16
16
|
# # good
|
17
17
|
# GetEnv.fetch(...)
|
18
|
-
class NoENV <
|
18
|
+
class NoENV < Base
|
19
|
+
extend AutoCorrector
|
20
|
+
|
19
21
|
MSG = 'Use `GetEnv` instead of `ENV`.'
|
20
22
|
|
21
23
|
def_node_matcher :is_ENV_index?, '(send (const nil? :ENV) :[] _key)'
|
@@ -25,11 +27,7 @@ module RuboCop
|
|
25
27
|
parent = node.parent
|
26
28
|
return unless is_ENV_index?(parent) || is_ENV_fetch?(parent)
|
27
29
|
|
28
|
-
add_offense(node)
|
29
|
-
end
|
30
|
-
|
31
|
-
def autocorrect(node)
|
32
|
-
lambda do |corrector|
|
30
|
+
add_offense(node) do |corrector|
|
33
31
|
if Gem::Version.new(RuboCop::Version.version) >= Gem::Version.new('0.82.0')
|
34
32
|
corrector.replace(node, 'GetEnv')
|
35
33
|
else
|
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
|
@@ -15,7 +15,7 @@ RSpec.describe RuboCop::Cop::Lint::NoENV do
|
|
15
15
|
it 'registers an offense when using `ENV`' do
|
16
16
|
expect_offense(<<~RUBY)
|
17
17
|
FOO = ENV['FOO']
|
18
|
-
^^^ Use `GetEnv` instead of `ENV`.
|
18
|
+
^^^ Lint/NoENV: Use `GetEnv` instead of `ENV`.
|
19
19
|
RUBY
|
20
20
|
|
21
21
|
expect_correction(<<~RUBY)
|
@@ -34,7 +34,7 @@ RSpec.describe RuboCop::Cop::Lint::NoENV do
|
|
34
34
|
it 'registers an offense when using `ENV`' do
|
35
35
|
expect_offense(<<~RUBY)
|
36
36
|
do_the_thing(ENV.fetch('FOO'))
|
37
|
-
^^^ Use `GetEnv` instead of `ENV`.
|
37
|
+
^^^ Lint/NoENV: Use `GetEnv` instead of `ENV`.
|
38
38
|
RUBY
|
39
39
|
|
40
40
|
expect_correction(<<~RUBY)
|
@@ -45,7 +45,7 @@ RSpec.describe RuboCop::Cop::Lint::NoENV do
|
|
45
45
|
it 'registers an offense when using `ENV` with a default value' do
|
46
46
|
expect_offense(<<~RUBY)
|
47
47
|
a = x + ENV.fetch('FOO', 42)
|
48
|
-
^^^ Use `GetEnv` instead of `ENV`.
|
48
|
+
^^^ Lint/NoENV: Use `GetEnv` instead of `ENV`.
|
49
49
|
RUBY
|
50
50
|
|
51
51
|
expect_correction(<<~RUBY)
|
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.2
|
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: 2025-01-22 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
|