Class: Confg::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: Confg.env, root: Confg.root) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
# File 'lib/confg/configuration.rb', line 10

def initialize(env: Confg.env, root: Confg.root)
  @confg_env = env.to_s
  @confg_root = Pathname.new(root)
  @confg_data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, **kwargs, &block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/confg/configuration.rb', line 126

def method_missing(key, *args, **kwargs, &block)
  key = key.to_s
  return set(key[0...-1], args[0]) if key.end_with?("=")
  return fetch(key) if key?(key)

  begin
    confg_data.send(key, *args, **kwargs, &block)
  rescue NoMethodError => e
    raise KeyError, "Unrecognized key `#{key}`", e.backtrace
  end
end

Instance Attribute Details

#confg_dataObject (readonly)

Returns the value of attribute confg_data.



8
9
10
# File 'lib/confg/configuration.rb', line 8

def confg_data
  @confg_data
end

#confg_envObject (readonly)

Returns the value of attribute confg_env.



8
9
10
# File 'lib/confg/configuration.rb', line 8

def confg_env
  @confg_env
end

#confg_rootObject (readonly)

Returns the value of attribute confg_root.



8
9
10
# File 'lib/confg/configuration.rb', line 8

def confg_root
  @confg_root
end

Instance Method Details

#fetch(key, &block) ⇒ Object



69
70
71
# File 'lib/confg/configuration.rb', line 69

def fetch(key, &block)
  confg_data.fetch(key.to_s, &block)
end

#get(key) ⇒ Object Also known as: []



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

def get(key)
  fetch(key) { nil }
end

#inspectObject



16
17
18
# File 'lib/confg/configuration.rb', line 16

def inspect
  "#<#{self.class.name} #{confg_data.inspect}>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/confg/configuration.rb', line 60

def key?(key)
  confg_data.key?(key.to_s)
end

#load_env(prefix: "CONFG_", separator: "__") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/confg/configuration.rb', line 35

def load_env(prefix: "CONFG_", separator: "__")
  overrides = {}

  ENV.each do |key, value|
    next unless key.start_with?(prefix)

    segments = key.delete_prefix(prefix).downcase.split(separator)

    # Build nested hash: ["database", "host"] => { "database" => { "host" => value } }
    current = overrides
    segments[0...-1].each do |segment|
      current = (current[segment] ||= {})
    end
    current[segments.last] = value
  end

  merge(overrides)
end

#load_key(key, yaml_loader_options = {}) ⇒ Object



93
94
95
96
# File 'lib/confg/configuration.rb', line 93

def load_key(key,yaml_loader_options = {})
  # loads yaml file with given key
  load_yaml(key, yaml_loader_options, key: key)
end

#load_yaml(path, yaml_loader_options = {}, key: nil, ignore_env: false) ⇒ Object Also known as: load_yml

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/confg/configuration.rb', line 98

def load_yaml(path, yaml_loader_options = {}, key: nil, ignore_env: false)
  found_path = find_config_yaml(path)

  raise ArgumentError, "#{path} could not be found" if found_path.nil?

  ctxt = ::Confg::ErbContext.new
  raw_content = ::File.read(found_path)
  erb_content = ctxt.evaluate(raw_content)
  yaml_content = ::YAML.safe_load(erb_content, **yaml_loader_options.merge(aliases: true)) # due to shared sections

  unless ignore_env
    yaml_content = yaml_content[confg_env] if confg_env && yaml_content.is_a?(::Hash) && yaml_content.key?(confg_env)
  end

  if key
    set(key, yaml_content)
  else
    if yaml_content.is_a?(Array)
      raise "A key must be provided to load the file at: #{found_path}"
    else
      yaml_content.each do |k, v|
        set(k, v)
      end
    end
  end
end

#merge(other) ⇒ Object Also known as: merge!



28
29
30
31
32
# File 'lib/confg/configuration.rb', line 28

def merge(other)
  other.each_pair do |k, v|
    set(k, v)
  end
end

#open(key) {|inner| ... } ⇒ Object

Yields:

  • (inner)


87
88
89
90
91
# File 'lib/confg/configuration.rb', line 87

def open(key)
  inner = get(key) || spawn_child
  yield(inner)
  set(key, inner)
end

#respond_to_missing?(key, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
# File 'lib/confg/configuration.rb', line 138

def respond_to_missing?(key, include_private = false)
  key = key.to_s
  return true if key.end_with?("=")
  return true if key?(key)

  confg_data.respond_to?(key, include_private)
end

#set(key, value = nil) ⇒ Object Also known as: []=



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/confg/configuration.rb', line 73

def set(key, value = nil)
  confg_data[key.to_s] = case value
  when ::Hash
    open(key) do |child|
      value.each_pair do |k, v|
        child.set(k, v)
      end
    end
  else
    value
  end
end

#tmp(key, value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/confg/configuration.rb', line 20

def tmp(key, value)
  initial = get(key)
  set(key, value)
  yield
ensure
  set(key, initial)
end

#to_hObject



54
55
56
57
58
# File 'lib/confg/configuration.rb', line 54

def to_h
  confg_data.transform_values do |v|
    v.is_a?(self.class) ? v.to_h : v
  end
end