class YamlChecker::Checker

Public Class Methods

new(argv) click to toggle source
# File lib/yaml_checker.rb, line 14
def initialize(argv)
  @argv = argv
end
run(*args) click to toggle source
# File lib/yaml_checker.rb, line 10
def self.run(*args)
  new(*args).run
end

Public Instance Methods

run() click to toggle source
# File lib/yaml_checker.rb, line 18
def run
  if File.directory?(path)
    Dir.glob("#{path}/**/*.{yml,yaml}").each do |f|
      load(f)
    end
    warn errors
    return errors.empty?
  elsif File.file?(path)
    if File.extname(path).match(/\.ya?ml/)
      load(path)
      warn errors
      return errors.empty?
    else
      warn "#{path}: File extname should be .yml or .yaml"
      return false
    end
  else
    warn "#{path}: No such file or directory"
    return false
  end
end

Private Instance Methods

errors() click to toggle source
# File lib/yaml_checker.rb, line 42
def errors
  @errors ||= []
end
load(file_name) click to toggle source
# File lib/yaml_checker.rb, line 46
def load(file_name)
  YAML.load_file(file_name)
rescue Exception => e
  errors << e
end
path() click to toggle source
# File lib/yaml_checker.rb, line 52
def path
  @path ||=
    if @argv.first.nil? || @argv.first.empty?
      warn usage
      abort "Missing file or directory path argument"
      @argv.first
    else
      File.expand_path(@argv.first)
    end
end
usage() click to toggle source
# File lib/yaml_checker.rb, line 63
    def usage
      <<-EOS
usage: yaml_checker <file(.yml/.yaml) or directory path>

      EOS
    end