Class: Comff

Inherits:
Object
  • Object
show all
Defined in:
lib/comff.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_content = nil) ⇒ Comff

Returns a new instance of Comff.



5
6
7
8
9
# File 'lib/comff.rb', line 5

def initialize(file_content = nil)
  @conf = nil

  load_conf_file!(file_content) if file_content
end

Class Method Details

.get_bool(*args) ⇒ Object



73
# File 'lib/comff.rb', line 73

def self.get_bool(*args); @global.get_bool(*args); end

.get_bool!(*args) ⇒ Object



72
# File 'lib/comff.rb', line 72

def self.get_bool!(*args); @global.get_bool!(*args); end

.get_int(*args) ⇒ Object



71
# File 'lib/comff.rb', line 71

def self.get_int(*args); @global.get_int(*args); end

.get_int!(*args) ⇒ Object



70
# File 'lib/comff.rb', line 70

def self.get_int!(*args); @global.get_int!(*args); end

.get_str(*args) ⇒ Object



69
# File 'lib/comff.rb', line 69

def self.get_str(*args); @global.get_str(*args); end

.get_str!(*args) ⇒ Object



68
# File 'lib/comff.rb', line 68

def self.get_str!(*args); @global.get_str!(*args); end

.load_global(file_content) ⇒ Object



64
65
66
# File 'lib/comff.rb', line 64

def self.load_global(file_content)
  @global = Comff.new(file_content)
end

Instance Method Details

#get_bool(key, default = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
# 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) ⇒ Object



30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



56
57
58
59
60
61
62
# 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