Add Pry::Env

Env is a helper module to work with environment variables.
This commit is contained in:
Kyrylo Silin 2019-06-09 16:38:07 +03:00
parent 17bdfd7082
commit 7ce5ca70bb
3 changed files with 45 additions and 0 deletions

View File

@ -28,6 +28,7 @@ require 'pry/system_command_handler'
require 'pry/control_d_handler'
require 'pry/command_state'
require 'pry/warning'
require 'pry/env'
Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands)

18
lib/pry/env.rb Normal file
View File

@ -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

26
spec/env_spec.rb Normal file
View File

@ -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