Class: Waiter::Menu

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/waiter/menu.rb,
lib/waiter/menu/item.rb,
lib/waiter/menu/drawer.rb,
lib/waiter/menu/section.rb,
lib/waiter/menu/item_list.rb

Defined Under Namespace

Classes: Drawer, Item, ItemList, Section

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#method_missing, #section

Constructor Details

#initialize(name, context, options = {}) ⇒ Menu

Returns a new instance of Menu.



20
21
22
23
24
25
# File 'lib/waiter/menu.rb', line 20

def initialize(name, context, options = {})
  @name = name
  @context = context
  @items = ItemList.new
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Waiter::DSL

Instance Attribute Details

#contextObject

Returns the value of attribute context.



10
11
12
# File 'lib/waiter/menu.rb', line 10

def context
  @context
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/waiter/menu.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/waiter/menu.rb', line 10

def options
  @options
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/waiter/menu.rb', line 10

def parent
  @parent
end

Returns the value of attribute submenu.



10
11
12
# File 'lib/waiter/menu.rb', line 10

def submenu
  @submenu
end

Class Method Details

.serve(name, context = nil, options = {}, &block) ⇒ Object



14
15
16
17
18
# File 'lib/waiter/menu.rb', line 14

def self.serve(name, context = nil, options = {}, &block)
  new(name, context, options).tap do |menu|
    menu.update(&block)
  end
end

Instance Method Details

#[](name) ⇒ Object



48
49
50
# File 'lib/waiter/menu.rb', line 48

def [](name)
  items[name]
end

#add(name, path = {}, options = {}, &block) ⇒ Object



35
36
37
# File 'lib/waiter/menu.rb', line 35

def add(name, path = {}, options = {}, &block)
  items << Item.new(self, name, path, options, &block)
end

#add_section(options = {}, &block) ⇒ Object



39
40
41
42
# File 'lib/waiter/menu.rb', line 39

def add_section(options = {}, &block)
  section = Section.new(self, options, &block)
  items << section if section.items.any?
end

#draw(context = nil) ⇒ Object



31
32
33
# File 'lib/waiter/menu.rb', line 31

def draw(context = nil)
  Drawer.new(self, context).draw
end

#inspectObject



61
62
63
# File 'lib/waiter/menu.rb', line 61

def inspect
  "#<#{self.class.name}:#{'0x%x' % (36971870 << 1)} name=#{name.inspect} options=#{options.inspect}>"
end

#items(sorted = false) ⇒ Object



52
53
54
55
# File 'lib/waiter/menu.rb', line 52

def items(sorted = false)
  return @items unless sorted
  ItemList.new(@items, options.slice(:sort, :reverse))
end

#sectionsObject



44
45
46
# File 'lib/waiter/menu.rb', line 44

def sections
  items.select(&:section?) || []
end

#top?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/waiter/menu.rb', line 57

def top?
  parent.nil?
end

#update(&block) ⇒ Object



27
28
29
# File 'lib/waiter/menu.rb', line 27

def update(&block)
  instance_eval(&block)
end