Module: YAML

Defined in:
lib/yaml_extend.rb

Overview

Extending the YAML library to allow to inherit from another YAML file(s)

Class Method Summary collapse

Class Method Details

.ext_load_file(yaml_path, inheritance_key = 'extends', extend_existing_arrays = true, config = {}) ⇒ Hash

Extended variant of the #load_file method by providing the ability to inherit from other YAML file(s)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yaml_extend.rb', line 25

def self.ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true, config = {})
  total_config ||= {}
  yaml_path = YAML.make_absolute_path yaml_path
  super_config = YAML.load_file(File.open(yaml_path))
  super_inheritance_files = super_config[inheritance_key]
  super_config.delete inheritance_key # we don't merge the super inheritance keys into the base yaml
  merged_config = config.clone.deeper_merge(super_config, extend_existing_arrays: extend_existing_arrays)
  if super_inheritance_files && super_inheritance_files != ''
    super_inheritance_files = [super_inheritance_files] unless super_inheritance_files.is_a? Array # we support strings as well as arrays of type string to extend from
    super_inheritance_files.each_with_index do |super_inheritance_file, index|
      super_config_path = File.dirname(yaml_path) + '/' + super_inheritance_file
      total_config = YAML.ext_load_file super_config_path, inheritance_key, extend_existing_arrays, total_config.deeper_merge(merged_config, extend_existing_arrays: extend_existing_arrays)
    end
    total_config
  else
    merged_config.delete(inheritance_key)
    merged_config
  end
end