-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Hashr | ||
module Env | ||
class Vars < Struct.new(:defaults, :namespace) | ||
FALSE = [false, nil, 'false', 'nil', ''] | ||
|
||
def to_h | ||
defaults.deep_merge(read_env(defaults, namespace.dup)) | ||
end | ||
|
||
private | ||
|
||
def read_env(defaults, namespace) | ||
defaults.inject({}) do |result, (key, default)| | ||
keys = namespace + [key] | ||
value = default.is_a?(Hash) ? read_env(default, keys) : var(keys, default) | ||
result.merge(key => value) | ||
end | ||
end | ||
|
||
def var(keys, default) | ||
key = keys.map(&:upcase).join('_') | ||
value = ENV.fetch(key, default) | ||
cast(value, default, keys) | ||
end | ||
|
||
def cast(value, default, keys) | ||
case default | ||
when Array | ||
value.respond_to?(:split) ? value.split(',') : Array(value) | ||
when true, false | ||
not FALSE.include?(value) | ||
else | ||
value | ||
end | ||
end | ||
|
||
def namespace | ||
Array(super && super.upcase) | ||
end | ||
end | ||
|
||
attr_accessor :env_namespace | ||
|
||
def defaults | ||
Vars.new(super, env_namespace).to_h | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
describe Hashr::Env do | ||
let(:klass) do | ||
Class.new(Hashr) do | ||
extend Hashr::Env | ||
|
||
self.env_namespace = 'hashr' | ||
|
||
define string: 'string', | ||
hash: { key: 'value' }, | ||
array: ['foo', 'bar'], | ||
bool: false | ||
end | ||
end | ||
|
||
after do | ||
ENV.keys.each { |key| ENV.delete(key) if key.start_with?('HASHR_') } | ||
end | ||
|
||
it 'defaults to an env var' do | ||
ENV['HASHR_STRING'] = 'env string' | ||
expect(klass.new.string).to eq('env string') | ||
end | ||
|
||
it 'defaults to a nested env var' do | ||
ENV['HASHR_HASH_KEY'] = 'env value' | ||
expect(klass.new.hash.key).to eq('env value') | ||
end | ||
|
||
describe 'type casts based on the default' do | ||
describe 'to boolean' do | ||
it 'true given' do | ||
ENV['HASHR_BOOL'] = 'true' | ||
expect(klass.new.bool).to eq(true) | ||
end | ||
|
||
it 'false given' do | ||
ENV['HASHR_BOOL'] = 'false' | ||
expect(klass.new.bool).to eq(false) | ||
end | ||
|
||
it 'empty string given' do | ||
ENV['HASHR_BOOL'] = '' | ||
expect(klass.new.bool).to eq(false) | ||
end | ||
end | ||
|
||
describe 'to an array' do | ||
it 'splits a comma-separated string' do | ||
ENV['HASHR_ARRAY'] = 'env foo,env bar' | ||
expect(klass.new.array).to eq(['env foo', 'env bar']) | ||
end | ||
|
||
it 'returns an empty array for an empty string' do | ||
ENV['HASHR_ARRAY'] = '' | ||
expect(klass.new.array).to eq([]) | ||
end | ||
end | ||
end | ||
|
||
it 'data takes precedence over an env var' do | ||
ENV['HASHR_STRING'] = 'env string' | ||
expect(klass.new(string: 'data string').string).to eq('data string') | ||
end | ||
end |