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.



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

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.



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

def context
  @context
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Returns the value of attribute submenu.



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

def submenu
  @submenu
end

Class Method Details

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



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

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



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

def [](name)
  items[name]
end

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



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

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

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



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

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

#draw(context = nil) ⇒ Object



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

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

#inspectObject



64
65
66
# File 'lib/waiter/menu.rb', line 64

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

#items(sorted = false) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/waiter/menu.rb', line 51

def items(sorted = false)
  return @items unless sorted

  items = @items.dup
  items.sort_by!(&options[:sort].to_sym) if options.fetch(:sort, false)
  items.reverse! if options.fetch(:reverse, false)
  ItemList.new(items)
end

#sectionsObject



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

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

#top?Boolean

Returns:

  • (Boolean)


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

def top?
  parent.nil?
end

#update(&block) ⇒ Object



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

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