Add Pry::Env
Env is a helper module to work with environment variables.
This commit is contained in:
parent
17bdfd7082
commit
7ce5ca70bb
|
@ -28,6 +28,7 @@ require 'pry/system_command_handler'
|
||||||
require 'pry/control_d_handler'
|
require 'pry/control_d_handler'
|
||||||
require 'pry/command_state'
|
require 'pry/command_state'
|
||||||
require 'pry/warning'
|
require 'pry/warning'
|
||||||
|
require 'pry/env'
|
||||||
|
|
||||||
Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands)
|
Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Pry
|
||||||
|
# Env is a helper module to work with environment variables.
|
||||||
|
#
|
||||||
|
# @since ?.?.?
|
||||||
|
# @api private
|
||||||
|
module Env
|
||||||
|
def self.[](key)
|
||||||
|
return unless ENV.key?(key)
|
||||||
|
|
||||||
|
value = ENV[key]
|
||||||
|
return if value == ''
|
||||||
|
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,26 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
RSpec.describe Pry::Env do
|
||||||
|
describe "#[]" do
|
||||||
|
let(:key) { 'PRYTESTKEY' }
|
||||||
|
|
||||||
|
after { ENV.delete(key) }
|
||||||
|
|
||||||
|
context "when ENV contains the passed key" do
|
||||||
|
before { ENV[key] = 'val' }
|
||||||
|
after { ENV.delete(key) }
|
||||||
|
|
||||||
|
specify { expect(described_class[key]).to eq('val') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when ENV doesn't contain the passed key" do
|
||||||
|
specify { expect(described_class[key]).to be_nil }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when ENV contains the passed key but its value is nil" do
|
||||||
|
before { ENV[key] = '' }
|
||||||
|
|
||||||
|
specify { expect(described_class[key]).to be_nil }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue