Class: Rdm::ConfigManager

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



55
56
57
# File 'lib/rdm/config_manager.rb', line 55

def method_missing(method_name, *args)
  root_scope.send(method_name, *args)
end

Instance Method Details

#load_config(config, source:) ⇒ Object

Update configuration based on given config file

Parameters:

Returns:

  • root scope [Rdm::ConfigScope] Updated root scope



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rdm/config_manager.rb', line 8

def load_config(config, source:)
  if config.default_path
    full_default_path = File.join(source.root_path, config.default_path)
    update_using_file(full_default_path, raise_if_missing: true)
  end
  if config.role_path
    full_role_path = File.join(source.root_path, config.role_path)
    update_using_file(full_role_path, raise_if_missing: false)
  end
  root_scope
end

#to_hObject



59
60
61
# File 'lib/rdm/config_manager.rb', line 59

def to_h
  root_scope.to_h
end

#update_using_file(path, raise_if_missing: true) ⇒ Object

Update configuration using given path to config file

Parameters:

  • config (Hash<String: AnyValue>)

    Hash with configuration

Returns:

  • root scope [Rdm::ConfigScope] Updated root scope



23
24
25
26
27
28
29
30
31
32
# File 'lib/rdm/config_manager.rb', line 23

def update_using_file(path, raise_if_missing: true)
  if File.exist?(path)
    compiled_file = ::ERB.new(File.read(path)).result
    hash = YAML.load(compiled_file)
    update_using_hash(hash)
  elsif raise_if_missing
    raise "Config file is not found at path #{path}"
  end
  root_scope
end

#update_using_hash(hash, scope: nil) ⇒ Object

Update configuration based on given hash

Parameters:

  • config (Hash<String: AnyValue>)

    Hash with configuration

Returns:

  • root scope [Rdm::ConfigScope] Updated root scope



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rdm/config_manager.rb', line 37

def update_using_hash(hash, scope: nil)
  scope ||= root_scope

  hash.each do |key, value|
    if value.is_a?(Hash)
      # Try using existing scope
      child_scope = scope.read_attribute(key)
      if !child_scope || !child_scope.is_a?(Rdm::ConfigScope)
        child_scope = Rdm::ConfigScope.new
      end
      update_using_hash(value, scope: child_scope)
      scope.write_attribute(key, child_scope)
    else
      scope.write_attribute(key, value)
    end
  end
end