Class: Grande::ConfigLoader
- Inherits:
-
Object
- Object
- Grande::ConfigLoader
- Defined in:
- lib/grande/config_loader.rb
Instance Method Summary collapse
- #get_int(key, default = nil) ⇒ Object
- #get_int!(key) ⇒ Object
- #get_str(key, default = nil) ⇒ Object
- #get_str!(key) ⇒ Object
-
#initialize ⇒ ConfigLoader
constructor
A new instance of ConfigLoader.
Constructor Details
#initialize ⇒ ConfigLoader
Returns a new instance of ConfigLoader.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Method Details
#get_int(key, default = nil) ⇒ Object
26 27 28 29 30 |
# 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) ⇒ Object
32 33 34 35 36 |
# 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) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
54 55 56 57 58 59 60 |
# 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 |