Module: YamlNormalizer::Helpers::Normalize

Included in:
Services::Check, Services::IsYaml, Services::Normalize
Defined in:
lib/yaml_normalizer/helpers/normalize.rb

Overview

This helper holds shared functionality to normalize a YAML string.

Instance Method Summary collapse

Instance Method Details

#normalize_yaml(yaml) ⇒ String

Transforms a given YAML string to a normalized format.

Examples:

class YamlWriter
  include YamlNormalizer::Helpers::Normalize

  def initialize(yaml)
    @yaml = normalize_yaml(yaml)
  end

  def write(file)
    File.open(file,'w') { |f| f.write(@yaml) }
  end
end

Parameters:

  • valid (String)

    YAML string

Returns:

  • (String)

    normalized YAML string



24
25
26
27
28
29
30
# File 'lib/yaml_normalizer/helpers/normalize.rb', line 24

def normalize_yaml(yaml)
  hashes = parse(yaml).transform
  hashes.map do |hash|
    hash.extend(Ext::SortByKey)
    hash.sort_by_key.to_yaml
  end.join
end