class Comff

Public Class Methods

get_bool(*args) click to toggle source
# File lib/comff.rb, line 73
def self.get_bool(*args); @global.get_bool(*args); end
get_bool!(*args) click to toggle source
# File lib/comff.rb, line 72
def self.get_bool!(*args); @global.get_bool!(*args); end
get_int(*args) click to toggle source
# File lib/comff.rb, line 71
def self.get_int(*args); @global.get_int(*args); end
get_int!(*args) click to toggle source
# File lib/comff.rb, line 70
def self.get_int!(*args); @global.get_int!(*args); end
get_str(*args) click to toggle source
# File lib/comff.rb, line 69
def self.get_str(*args); @global.get_str(*args); end
get_str!(*args) click to toggle source
# File lib/comff.rb, line 68
def self.get_str!(*args); @global.get_str!(*args); end
load_global(file_content) click to toggle source
# File lib/comff.rb, line 64
def self.load_global(file_content)
  @global = Comff.new(file_content)
end
new(file_content = nil) click to toggle source
# File lib/comff.rb, line 5
def initialize(file_content = nil)
  @conf = nil

  load_conf_file!(file_content) if file_content
end

Public Instance Methods

get_bool(key, default=nil) click to toggle source
# File lib/comff.rb, line 11
def get_bool(key, default=nil)
  raw_value = get_str(key)
  return default if raw_value == nil

  value = raw_value&.to_s&.downcase

  return true if value == "true"
  return false if value == "false"

  raise "Invalid value '#{raw_value}' for key #{key}"
end
get_bool!(key) click to toggle source
# File lib/comff.rb, line 23
def get_bool!(key)
  value = get_bool(key)
  raise "Config key #{key} is required" if value == nil

  value
end
get_int(key, default=nil) click to toggle source
# File lib/comff.rb, line 30
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/comff.rb, line 36
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/comff.rb, line 42
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

  base_obj.fetch(parts.last, default)
end
get_str!(key) click to toggle source
# File lib/comff.rb, line 56
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/comff.rb, line 79
def key_to_env_var_name(key)
  key.split('.').map(&:upcase).join('_')
end
load_conf_file!(file_content) click to toggle source
# File lib/comff.rb, line 83
def load_conf_file!(file_content)
  @conf = YAML.safe_load(file_content)
end