Class: Koi::Menu::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/koi/menu/builder.rb

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



6
7
8
9
10
# File 'lib/koi/menu/builder.rb', line 6

def initialize
  @menu  = Katalyst::Navigation::Menu.new
  @index = 0
  @depth = 0
end

Instance Method Details

#add_button(title:, url:, **options) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/koi/menu/builder.rb', line 52

def add_button(title:, url:, **options)
  @menu.items.build(type:  Katalyst::Navigation::Button.name,
                    title:,
                    url:,
                    **options,
                    index: @index,
                    depth: @depth)
  @index += 1
end

#add_item(title, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/koi/menu/builder.rb', line 18

def add_item(title, value)
  if value.is_a?(Hash)
    add_menu(title:) do |b|
      value.each do |k, v|
        b.add_item(k, v)
      end
    end
  else
    add_link(title:, url: value)
  end
end

#add_items(items) ⇒ Object



12
13
14
15
16
# File 'lib/koi/menu/builder.rb', line 12

def add_items(items)
  items.each do |k, v|
    add_item(k, v)
  end
end


42
43
44
45
46
47
48
49
50
# File 'lib/koi/menu/builder.rb', line 42

def add_link(title:, url:, **options)
  @menu.items.build(type:  Katalyst::Navigation::Link.name,
                    title:,
                    url:,
                    **options,
                    index: @index,
                    depth: @depth)
  @index += 1
end

#add_menu(title:, **options) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/koi/menu/builder.rb', line 30

def add_menu(title:, **options, &)
  @menu.items.build(type:  Katalyst::Navigation::Heading.name,
                    title:,
                    **options,
                    index: @index,
                    depth: @depth)
  @index += 1
  @depth += 1
  yield(self)
  @depth -= 1
end

#renderObject



62
63
64
65
# File 'lib/koi/menu/builder.rb', line 62

def render
  @menu.published_version = @menu.build_draft_version
  @menu
end