Module: YAML

Defined in:
lib/yaml_extend.rb

Overview

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

Constant Summary collapse

DEFAULT_INHERITANCE_KEY =

default path in the yaml file where the files to inherit from are defined

'extends'
@@ext_load_key =
nil

Class Method Summary collapse

Class Method Details

.ext_load_file(yaml_path, inheritance_key = nil, extend_existing_arrays = true) ⇒ Hash

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

Parameters:

  • yaml_path (String)

    the path to the yaml file to be loaded

  • inheritance_key (String|Array) (defaults to: nil)

    The key used in the yaml file to extend from another YAML file. Use an Array if you want to use a tree structure key like “options.extends” => [‘options’,‘extends’]

  • extend_existing_arrays (Boolean) (defaults to: true)

    extend existing arrays instead of replacing them

Returns:

  • (Hash)

    the resulting yaml config



45
46
47
# File 'lib/yaml_extend.rb', line 45

def self.ext_load_file(yaml_path, inheritance_key=nil, extend_existing_arrays=true)
  YAML.ext_load_file_recursive(yaml_path, inheritance_key, extend_existing_arrays, {})
end

.ext_load_key=(key) ⇒ Object

Set a custom inheritance key globally once. So you don’t need to specify it on every call of ext_load_file()

Parameters:

  • key (String|Array<String>|nil)

    the key in the yaml file, containing the file strings to extend from. Set nil or call #reset_load_key to reset the key.



21
22
23
24
25
26
27
# File 'lib/yaml_extend.rb', line 21

def self.ext_load_key=(key)
  if key.is_a?(String) || key.is_a?(Array) || key.nil?
    @@ext_load_key = key
  else
    raise "Parameter 'key' must be of type String or Array<String> or nil"
  end
end

.reset_load_keyObject

Reset the ext_load_key and use the default settings



32
33
34
# File 'lib/yaml_extend.rb', line 32

def self.reset_load_key()
  @@ext_load_key = nil
end