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'
DEEP_MERGE_OPTIONS =
[
  :preserve_unmergeables,
  :knockout_prefix,
  :overwrite_arrays,
  :sort_merged_arrays,
  :unpack_arrays,
  :merge_hash_arrays,
  :extend_existing_arrays,
  :merge_nil_values,
  :merge_debug,
]
@@ext_load_key =
nil

Class Method Summary collapse

Class Method Details

.ext_load_file(yaml_path, inheritance_key = nil, options = {}) ⇒ Hash

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

deep merge options that can be passed by - but the defaults differ from original github.com/danielsdeleo/deep_merge#options

Parameters:

  • yaml_path (String|Pathname)

    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’]

  • options (Hash) (defaults to: {})

    to pass, including deep_merge options as well as

  • options (Boolean) (defaults to: {})

    Fallback for backward compatiblity: extend existing arrays instead of replacing them (deep_merge)

Options Hash (options):

  • :preserve_inheritance_key (Boolean)

    Preserve inheritance key(s) from resulting yaml, does most time not make sense especially in multiple inheritance - DEFAULT: false

  • :preserve_unmergeables (Boolean)

    Set to true to skip any unmergeable elements from source - DEFAULT: false

  • :knockout_prefix (String)

    Set to string value to signify prefix which deletes elements from existing element - DEFAULT: nil

  • :overwrite_arrays (Boolean)

    Set to true if you want to avoid merging arrays - DEFAULT: false

  • :sort_merged_arrays (Boolean)

    Set to true to sort all arrays that are merged together - DEFAULT: false

  • :unpack_arrays (String)

    Set to string value to run “Array::join” then “String::split” against all arrays - DEFAULT: nil

  • :merge_hash_arrays (Boolean)

    Set to true to merge hashes within arrays - DEFAULT: false

  • :extend_existing_arrays (Boolean)

    Set to true to extend existing arrays, instead of overwriting them - DEFAULT: true

  • :merge_nil_values (Boolean)

    Set to true to merge nil hash values, overwriting a possibly non-nil value - DEFAULT: false

  • :merge_debug (Boolean)

    Set to true to get console output of merge process for debugging - DEFAULT: false

Returns:

  • (Hash)

    the resulting yaml config



74
75
76
# File 'lib/yaml_extend.rb', line 74

def self.ext_load_file(yaml_path, inheritance_key = nil, options = {})
  YAML.ext_load_file_recursive(yaml_path, inheritance_key, options, {})
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.



34
35
36
37
38
39
40
# File 'lib/yaml_extend.rb', line 34

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



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

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