Sha256: 037de9845935ef5acb3f85c74215d78acdb28b16bea1db411914079693b160db

Contents?: true

Size: 1.61 KB

Versions: 26

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Koi
  module Menu
    class Builder
      def initialize
        @menu  = Katalyst::Navigation::Menu.new
        @index = 0
        @depth = 0
      end

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

      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

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

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

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

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
katalyst-koi-4.14.1 lib/koi/menu/builder.rb
katalyst-koi-4.14.0 lib/koi/menu/builder.rb
katalyst-koi-4.13.2 lib/koi/menu/builder.rb
katalyst-koi-4.13.0 lib/koi/menu/builder.rb
katalyst-koi-4.12.6 lib/koi/menu/builder.rb
katalyst-koi-4.12.5 lib/koi/menu/builder.rb
katalyst-koi-4.12.4 lib/koi/menu/builder.rb
katalyst-koi-4.12.3 lib/koi/menu/builder.rb
katalyst-koi-4.12.2 lib/koi/menu/builder.rb
katalyst-koi-4.12.1 lib/koi/menu/builder.rb
katalyst-koi-4.12.0 lib/koi/menu/builder.rb
katalyst-koi-4.11.2 lib/koi/menu/builder.rb
katalyst-koi-4.11.1 lib/koi/menu/builder.rb
katalyst-koi-4.11.0 lib/koi/menu/builder.rb
katalyst-koi-4.10.3 lib/koi/menu/builder.rb
katalyst-koi-4.10.2 lib/koi/menu/builder.rb
katalyst-koi-4.10.1 lib/koi/menu/builder.rb
katalyst-koi-4.10.0 lib/koi/menu/builder.rb
katalyst-koi-4.9.5 lib/koi/menu/builder.rb
katalyst-koi-4.9.4 lib/koi/menu/builder.rb