class YamlNormalizer::Services::Check

Check is a service class that provides functionality to check if giving YAML files are already standardized (normalized). @exmaple

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

Attributes

files[R]

files is a sorted array of file path Strings

Public Class Methods

new(*args) click to toggle source

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

# File lib/yaml_normalizer/services/check.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/check.rb, line 29
def call
  success = files.pmap { |file| process(file) }
  success.all?
end

Private Instance Methods

normalized?(file) click to toggle source
# File lib/yaml_normalizer/services/check.rb, line 44
def normalized?(file)
  file = relative_path_for(file)
  input = read(file)
  norm = normalize_yaml(input)
  check = input.eql?(norm)

  if check
    $stdout.print "[PASSED] already normalized #{file}\n"
  else
    $stdout.print "[FAILED] normalization suggested for #{file}\n"
  end

  check
end
process(file) click to toggle source

process returns true on success and nil on error

# File lib/yaml_normalizer/services/check.rb, line 37
def process(file)
  return true if IsYaml.call(file) && normalized?(file)

  $stderr.print "#{file} not a YAML file\n"
  nil
end