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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 840c77f6c35baeec3560a6fb8b64667fffe6e477d344c0b4d92e2b18f9a89758
4
- data.tar.gz: '0916cd555771f013375a4d42b190ee04736e101a0e1a9e963eb004cc899cb9bf'
3
+ metadata.gz: a69954781ff0a92ff24cbc9200e0605104f7df6228b0f389ea1400af81bbf926
4
+ data.tar.gz: 1b316b87f08984ced9e07c55bf3bf94f13b9a6b68cc7441a4cf05af77f6cee4f
5
5
  SHA512:
6
- metadata.gz: c636397567c57c93a12cbefc88bbed42e42fc6511a488971840af66a91b2aa8de92173412b9ba88ab9aee689c10307536181723f5e8d1a1fada132778ac0c3cd
7
- data.tar.gz: b1b43cea47ab2af9a9f1adbfda13319e0598d4a73566529dcfc8e4496276903804f295feaf4e8a4ca2b0132917cd9d3a3c50ea29c31d3f8dc08698266690bf4c
6
+ metadata.gz: 398a9044aa2d6982f27eaf7a3bd56d7bdb4c690fd5a0d79a166b54ecb4775fc6520a1f5133bc106e440f17162bb148157672c11c55eb5ba0a0a7b02e6b8d2d02
7
+ data.tar.gz: '036692057e61f8c0e600bcf296e8b161a0122650d4ed02fb0cb049ee805f0e053f6b0a0f98ca50a99994e86a49abf2d0f54b6a66a264650585b561d0824082c9'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GetEnv
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/get_env.rb CHANGED
@@ -3,30 +3,48 @@
3
3
  require 'get_env/version'
4
4
 
5
5
  module GetEnv
6
- def self.[](key)
7
- return nil if key.nil?
6
+ class << self
7
+ def [](key)
8
+ return nil if key.nil?
8
9
 
9
- v = ENV[key].to_i
10
- return v if v.to_s == ENV[key]
10
+ v = ENV[key].to_i
11
+ return v if v.to_s == ENV[key]
11
12
 
12
- v = ENV[key].to_f
13
- return v if v.to_s == ENV[key]
13
+ v = ENV[key].to_f
14
+ return v if v.to_s == ENV[key]
14
15
 
15
- return false if ENV[key] == 'false'
16
- return true if ENV[key] == 'true'
16
+ return false if ENV[key] == 'false'
17
+ return true if ENV[key] == 'true'
17
18
 
18
- ENV[key]
19
- end
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
- def self.fetch(key, default = nil)
22
- if ENV.has_key?(key)
23
- self[key]
24
- elsif !default.nil?
25
- default
26
- elsif block_given?
27
- yield
28
- else
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.0
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: 2020-05-26 00:00:00.000000000 Z
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.0.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