Sha256: dcc80f591be5300dfa824412c37523c3e076d399000fadb986be35c120479129

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require "yaml"

module Hasmenu
  class Builder
    include Printer

    def initialize(type, options)
      @type = type
      @location = options[:location] || Dir.pwd
    end

    def build_menu(restaurant, menu_uid)
      output = {}

      output["restaurant"] = restaurant

      chain = restaurant["chain"]
      menus = menu_uid ? "#{menu_uid}.yml" : "*.yml"
      pattern = File.join(@location, "data", "menu", "chain", chain, menus)

      Dir.glob(pattern).each do |menu|
        chain_menu = YAML.load_file(menu)
        output["restaurant"]["menus"] = chain_menu["chain"]["menus"]

        restaurant_dir = File.join(@location, "data", "menu", "restaurant", restaurant["uid"])
        FileUtils.mkdir_p(restaurant_dir) unless File.directory?(restaurant_dir)

        menu_file = File.join(restaurant_dir, File.basename(menu))
        File.open(menu_file, "w") { |f| f.write output.to_yaml }
        print_build_for restaurant["uid"]
      end
    end

    def build(uid, menu_uid)
      restaurants_yml = File.join(@location, "data", "restaurants.yml")
      unless File.exist? restaurants_yml
        print_invalid_path
        return
      end
      restaurants = YAML.load_file(restaurants_yml)

      case @type
        when "chain"
          restaurants.select! { |r| r["chain"] == uid }
        when "restaurant"
          restaurants.select! { |r| r["uid"] == uid }
        when "all"
          restaurants
        else
          print_invalid_build
          return
      end

      print_build_start
      restaurants.each do |restaurant|
        build_menu restaurant, menu_uid
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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