class Grande::ConfigLoader
Public Class Methods
new()
click to toggle source
# File lib/grande/config_loader.rb, line 5 def initialize @conf = nil default_path = File.join(Grande.c.app.root_path, 'config/config.yml') if ENV.key?('GRANDE_CONFIG_PATH') path = ENV['GRANDE_CONFIG_PATH'] if !File.exist?(path) raise "GRANDE_CONFIG_PATH set but file does not exist. Either remove the env var or create the file" end Grande.logger.info("Loading configuration from #{path}") load_conf_file!(path) elsif File.exist?(default_path) Grande.logger.info("Found config.yml at #{path}. Will load config from that") load_conf_file!(default_path) else Grande.logger.info('Configuration file not provided. Loading all configuration from environment variables') end end
Public Instance Methods
get_int(key, default=nil)
click to toggle source
# File lib/grande/config_loader.rb, line 26 def get_int(key, default=nil) value = get_str(key) return default if value == nil Integer(value) end
get_int!(key)
click to toggle source
# File lib/grande/config_loader.rb, line 32 def get_int!(key) value = get_int(key) raise "Config key #{key} is required" unless value Integer(value) end
get_str(key, default=nil)
click to toggle source
# File lib/grande/config_loader.rb, line 38 def get_str(key, default=nil) env_var_name = key_to_env_var_name(key) return ENV[env_var_name] if ENV.key?(env_var_name) parts = key.split('.') base_obj = if parts.length == 1 @conf else @conf.dig(*parts[0...-1]) end # binding.pry if key =="redis.url" base_obj.fetch(parts.last, default) end
get_str!(key)
click to toggle source
# File lib/grande/config_loader.rb, line 54 def get_str!(key) none = Object.new value = get_str(key, none) raise "Config key #{key} is required" if value == none value end
Private Instance Methods
key_to_env_var_name(key)
click to toggle source
Convert key format from foo.bar.qux into FOO_BAR_QUX
# File lib/grande/config_loader.rb, line 66 def key_to_env_var_name(key) key.split('.').map(&:upcase).join('_') end
load_conf_file!(path)
click to toggle source
# File lib/grande/config_loader.rb, line 70 def load_conf_file!(path) @conf = YAML.safe_load(File.read(path)) end