class Enfig
Attributes
'overwrite?'[RW]
enable_overwrite[RW]
env[RW]
files[RW]
root[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/enfig.rb, line 9 def initialize(args = {}) @config = nil @env = args[:env] @root = args[:root] || '' @files = args[:files] || [args[:file]].compact @enable_overwrite = args[:overwrite] == false ? false : true end
update!(args = {})
click to toggle source
# File lib/enfig.rb, line 42 def self.update!(args = {}) enfig = Enfig.new(args) enfig.update_env! enfig end
Public Instance Methods
[](key)
click to toggle source
# File lib/enfig.rb, line 21 def [](key) config[key] end
config()
click to toggle source
# File lib/enfig.rb, line 17 def config @config ||= load_config end
load_config()
click to toggle source
# File lib/enfig.rb, line 25 def load_config conf = {} files.each do |file| name = File.basename(file, '.yml').to_s.downcase.gsub(/[^a-z0-9_]/i, '_') conf[name.to_sym] = load_yaml(file) end conf end
update_env!()
click to toggle source
# File lib/enfig.rb, line 36 def update_env! load_env_hash(config).each do |k, v| ENV[k] = v.to_s if ENV[k].nil? || ENV[k] == '' || overwrite? end end
Private Instance Methods
load_env_hash(config_src, target = {}, prefix = nil)
click to toggle source
# File lib/enfig.rb, line 56 def load_env_hash(config_src, target = {}, prefix = nil) prefix.concat('_') if prefix && !prefix.end_with?('_') config_src.each do |key, value| suffix = key.to_s.upcase target_key = "#{prefix}#{suffix}" if value.is_a?(Hash) load_env_hash(value, target, target_key) else target[target_key] = value end end target end
load_yaml(*args)
click to toggle source
# File lib/enfig.rb, line 50 def load_yaml(*args) config = YAML.load_file(File.join(root, *args)) data = env.nil? ? config : config[env] symbolize_keys(data) end
symbolize_keys(hash)
click to toggle source
# File lib/enfig.rb, line 71 def symbolize_keys(hash) transform_keys(hash) { |key| key.to_sym rescue key } end
transform_keys(hash) { |key| ... }
click to toggle source
# File lib/enfig.rb, line 75 def transform_keys(hash, &block) result = {} hash.each do |key, value| result[yield(key)] = value.is_a?(Hash) ? transform_keys(value, &block) : value end result end