Class: Hasmenu::Builder

Inherits:
Object
  • Object
show all
Includes:
Printer
Defined in:
lib/hasmenu/builder.rb

Instance Method Summary collapse

Methods included from Printer

#print_build_for, #print_build_start, #print_format_for, #print_format_start, #print_header, #print_invalid_build, #print_invalid_format, #print_invalid_path, #print_invalid_report, #print_invalid_sequence, #print_invalid_version, #print_report, #print_warn_repeats

Constructor Details

#initialize(type, options) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
# File 'lib/hasmenu/builder.rb', line 7

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

Instance Method Details

#build(uid, menu_uid) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hasmenu/builder.rb', line 34

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

#build_menu(restaurant, menu_uid) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hasmenu/builder.rb', line 12

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