Skip to content

Commit

Permalink
Add ENVied.[]
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed Oct 5, 2018
1 parent 0682055 commit 93ed64d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/envied.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def self.env!(requested_groups, options = {})
end
end

# Allow to retrieve variable by it's name
#
# @example
# ENVied["RACK_ENV"] # => "development"
def self.[](name)
env && env[name.to_s]
end

def self.error_on_missing_variables!(options = {})
names = env.missing_variables.map(&:name)
if names.any?
Expand Down Expand Up @@ -81,7 +89,7 @@ def self.spring_enabled?
end

def self.method_missing(method, *args, &block)
respond_to_missing?(method) ? (env && env[method.to_s]) : super
respond_to_missing?(method) ? self[method.to_s] : super
end

def self.respond_to_missing?(method, include_private = false)
Expand Down
4 changes: 3 additions & 1 deletion lib/envied/env_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def variables_by_name
end

def [](name)
coerce(variables_by_name[name.to_sym])
variable = variables_by_name[name.to_sym]
raise ArgumentError, "No variable #{name} is defined" unless variable
coerce(variable)
end

def has_key?(name)
Expand Down
11 changes: 11 additions & 0 deletions spec/envied_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,16 @@ def envied_require(*args)
end
end
end

describe '::[]' do
before do
configured_with(name: :string).and_ENV({'name' => 'value'})
envied_require
end

it 'delegates call to env!' do
expect(described_class['name']).to eq('value')
end
end
end
end

0 comments on commit 93ed64d

Please sign in to comment.