This repository has been archived on 2022-02-03. You can view files and clone it, but cannot push or open issues or pull requests.
senf/src/senf.cr

39 lines
708 B
Crystal

module Senf
VERSION = {{ `shards version`.stringify.chomp.downcase }}
class SafeEnv
private property keys
def initialize(@keys = {} of String => String?)
end
def initialize(keys : Array(String))
@keys = {} of String => String?
keys.each { |k| self.<< k }
end
def <<(key : String) : self
@keys[key] = ENV[key]?
self
end
def []?(key : String) : String?
if @keys.has_key?(key)
@keys[key]?
end
end
def [](key : String) : String
if @keys.has_key?(key)
val = @keys[key]?
raise "ENV[#{key}] is nil" unless val
val
else
raise "No such key: #{key}"
end
end
end
end