Class: Phlexi::Menu::Builder

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

Overview

Builder class for constructing hierarchical menu structures. Provides a DSL for creating nested menu items with support for labels, URLs, icons, and badges.

Examples:

Basic usage

menu = Phlexi::Menu::Builder.new do |m|
  m.item "Home", url: "/"
  m.item "Products", url: "/products" do |products|
    products.item "All Products", url: "/products"
    products.item "Add Product", url: "/products/new"
  end
end

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|builder| ... } ⇒ Builder

Initializes a new menu builder.

Yields:

  • (builder)

    Passes the builder instance to the block for menu construction

Yield Parameters:



28
29
30
31
32
# File 'lib/phlexi/menu/builder.rb', line 28

def initialize(&)
  @items = []

  yield self if block_given?
end

Instance Attribute Details

#itemsArray<Phlexi::Menu::Item> (readonly)

Returns The collection of top-level menu items.

Returns:



19
20
21
# File 'lib/phlexi/menu/builder.rb', line 19

def items
  @items
end

Instance Method Details

#inspectString

Returns a string representation of the menu structure.

Returns:

  • (String)

    A human-readable representation of the menu



53
54
55
# File 'lib/phlexi/menu/builder.rb', line 53

def inspect
  "#<#{self.class} items=#{@items.map(&:inspect)}>"
end

#item(label) {|item| ... } ⇒ Phlexi::Menu::Item

Creates and adds a new menu item to the current menu level.

Parameters:

  • label (String)

    The display text for the menu item

  • ** (Hash)

    Additional options passed to the Item constructor

Yields:

  • (item)

    Optional block for adding nested menu items

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the label is nil



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

def item(label, **, &)
  raise ArgumentError, "Label cannot be nil" unless label

  new_item = self.class::Item.new(label, **, &)
  @items << new_item
  new_item
end