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
# File 'lib/yaml_normalizer/helpers/normalize.rb', line 24

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