class Chambermaid::Environment
Environment
keeps a set of params available to load into ENV. It also maintains a copy of ENV at the time of its initialization, in order to restore it.
@attr_reader [Hash] params
Attributes
params[R]
Public Class Methods
new(params)
click to toggle source
Create new Chambermaid::Environment
@param [Hash] params
@raise [ArgumentError]
if params is not type Hash
@return [Chambermaid::Environment]
# File lib/chambermaid/environment.rb, line 18 def initialize(params) validate_params!(params) stash_current_env! update(format_env(params)) end
Public Instance Methods
load!()
click to toggle source
Inject into ENV without overwriting duplicates
@return [Hash]
# File lib/chambermaid/environment.rb, line 45 def load! each { |k, v| ENV[k] ||= v } end
overload!()
click to toggle source
Inject into ENV and overwrite duplicates
@return [Hash]
# File lib/chambermaid/environment.rb, line 52 def overload! each { |k, v| ENV[k] = v } end
to_dotenv()
click to toggle source
Generate a dotenv (.env) compatible string
@return [String] dotenv compatible string
# File lib/chambermaid/environment.rb, line 27 def to_dotenv to_h.inject("") do |env_str, param| env_str + "#{param[0]}=#{param[1]}\n" end end
to_file!(file_path)
click to toggle source
Write a .env file
@param [String] file_path
# File lib/chambermaid/environment.rb, line 36 def to_file!(file_path) File.open(file_path, "wb") do |f| f.write(to_dotenv) end end
unload!()
click to toggle source
Restore to original ENV
@return [ENV]
# File lib/chambermaid/environment.rb, line 59 def unload! ENV.replace(@_original_env) end
Private Instance Methods
format_env(params)
click to toggle source
# File lib/chambermaid/environment.rb, line 69 def format_env(params) params.map{|k,v| [k.upcase, v]}.to_h end
stash_current_env!()
click to toggle source
# File lib/chambermaid/environment.rb, line 65 def stash_current_env! @_original_env ||= ENV.to_h.dup.freeze end
validate_params!(params)
click to toggle source
# File lib/chambermaid/environment.rb, line 73 def validate_params!(params) unless params.is_a?(Hash) raise ArgumentError.new("`params` must be a hash") end end