lib/hasmenu/formatter.rb in hasmenu-0.1.5 vs lib/hasmenu/formatter.rb in hasmenu-0.1.6

- old
+ new

@@ -2,17 +2,34 @@ module Hasmenu class Formatter include Printer - def initialize(options) + def initialize(type, options) + @type = type @location = options[:location] || Dir.pwd end + def valid_path?(path) + (File.file?(path) && File.extname(path) == ".yml") || File.directory?(path) + end + + def format_list(path) + data = YAML.load_file(path) + unless data.is_a? Array + print_invalid_path + return + end + + data.sort_by! { |e| [e["chain"], e["uid"]] } + File.open(path, "w") { |f| f.write data.to_yaml } + print_format_for File.basename(path) + end + def format_file(path) - file = YAML.load_file(path) - File.open(path, "w") { |f| f.write file.to_yaml } + data = YAML.load_file(path) + File.open(path, "w") { |f| f.write data.to_yaml } print_format_for File.basename(File.dirname(path)) end def format_files(path) Dir.glob(path + "/**/*.yml") do |file| @@ -21,21 +38,26 @@ end def format(path) path = File.join(@location, path) - unless File.exist? path + unless valid_path? path print_invalid_path return end print_format_start - if File.file?(path) && File.extname(path) == ".yml" - format_file path - elsif File.directory? path - format_files path - else - print_invalid_path + case @type + when "chains", "restaurants" + format_list path + when "menu" + if File.directory? path + format_files path + else + format_file path + end + else + print_invalid_format end end end end