class YamlNormalizer::Services::Normalize

Normalize is a service class that provides functionality to update giving YAML files to a standardized (normalized) format. @exmaple

normalize = YamlNormalizer::Services::Normalize.new('path/to/*.yml')
result = normalize.call

Attributes

files[R]

files is a sorted array of file path Strings

Public Class Methods

new(*args) click to toggle source

Create a Normalize service object by calling .new and passing one or more String that are interpreted as file glob pattern. @param *args [Array<String>] a list of file glob patterns

# File lib/yaml_normalizer/services/normalize.rb, line 22
def initialize(*args)
  parse_params(*args)
  files = args.each_with_object([]) { |a, o| o << Dir[a.to_s] }
  @files = files.flatten.sort.uniq
end

Public Instance Methods

call() click to toggle source

Normalizes all YAML files defined on instantiation.

# File lib/yaml_normalizer/services/normalize.rb, line 29
def call
  files.peach { |file| process(file) }
end

Private Instance Methods

convert(yaml) click to toggle source
# File lib/yaml_normalizer/services/normalize.rb, line 59
def convert(yaml)
  ary = Psych.parse_stream(yaml).transform
  ary.each { |hash| hash.extend(Ext::Namespaced) }
  ary
end
normalize!(file) click to toggle source
# File lib/yaml_normalizer/services/normalize.rb, line 43
def normalize!(file)
  file = relative_path_for(file)
  if stable?(input = read(file), norm = normalize_yaml(input))
    File.open(file, 'w') { |f| f.write(norm) }
    $stderr.print "[NORMALIZED] #{file}\n"
  else
    $stderr.print "[ERROR]      Could not normalize #{file}\n"
  end
end
process(file) click to toggle source
# File lib/yaml_normalizer/services/normalize.rb, line 35
def process(file)
  if IsYaml.call(file)
    normalize!(file)
  else
    $stderr.print "#{file} not a YAML file\n"
  end
end
stable?(yaml_a, yaml_b) click to toggle source
# File lib/yaml_normalizer/services/normalize.rb, line 53
def stable?(yaml_a, yaml_b)
  convert(yaml_a).each_with_index.all? do |a, i|
    a.namespaced.eql?(convert(yaml_b).fetch(i).namespaced)
  end
end