Sha256: 0ba89adf80d289f98101ce75d4694b9ee9c269e987d2ed0d74e1c9ef9955bf7b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require "yaml"

module Hasmenu
  class Formatter
    include Printer

    def initialize(type, options)
      @type = type.chomp("/")
      @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)
      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(File.join(path, "**", "*.yml")) do |file|
        format_file file
      end
    end

    def format(path)
      path = File.join(@location, path)

      unless valid_path? path
        print_invalid_path
        return
      end

      print_format_start
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hasmenu-0.1.7 lib/hasmenu/formatter.rb