class Navigator::Menu

Renders a HTML menu.

Constants

ALLOWED_ELEMENTS

Public Class Methods

new(template, tag: "ul", attributes: {}) click to toggle source
# File lib/navigator/menu.rb, line 34
def initialize template,
               tag: "ul",
               attributes: {},
               activator: Navigator::TagActivator.new,
               &block

  @template = template
  @tag = Navigator::Tag.new(tag, attributes:, activator:)
  @menu_activator = activator
  @items = []
  instance_eval(&block) if block
end

Public Instance Methods

activate_item_attributes!(attributes, url, activator) click to toggle source
# File lib/navigator/menu.rb, line 103
def activate_item_attributes! attributes, url, activator
  return unless url == activator.search_value

  key = [attributes[activator.target_key], activator.target_value].compact.join " "
  attributes[activator.target_key] = key
end
add(name, content = nil, attributes: {}) click to toggle source
# File lib/navigator/menu.rb, line 47
def add name, content = nil, attributes: {}, activator: menu_activator, &block
  tag = Navigator::Tag.new(name, content, attributes:, activator:)
  return items << tag.render unless block

  items << tag.prefix
  items << tag.content
  instance_eval(&block)
  items << tag.suffix
end
image(url, alt = nil, attributes: {}) click to toggle source
# File lib/navigator/menu.rb, line 61
def image url, alt = nil, attributes: {}, activator: menu_activator
  modified_attributes = attributes.merge(src: url, alt:)
  modified_attributes = modified_attributes.delete_if { |_, value| !value.present? }

  add "img", attributes: modified_attributes, activator:
end
item(content = nil, url, item_attributes: {}) click to toggle source
# File lib/navigator/menu.rb, line 68
def item content = nil,
         url,
         item_attributes: {},
         link_attributes: {},
         activator: menu_activator,
         &block

  modified_item_attributes = item_attributes.clone
  activate_item_attributes! modified_item_attributes, url, activator

  add "li", attributes: modified_item_attributes, activator: do
    link content,
         url,
         attributes: link_attributes,
         activator: Navigator::TagActivator.new,
         &block
  end
end
method_missing(name, *positionals, **keywords, &) click to toggle source
Calls superclass method
# File lib/navigator/menu.rb, line 87
def method_missing(name, *positionals, **keywords, &)
  if respond_to_missing? name
    add(name, *positionals, **keywords, &)
  else
    template.public_send(name, *positionals, **keywords) || super
  end
end
render(= [tag.prefix, tag.content, items.compact.join, tag.suffix].compact.join) click to toggle source
# File lib/navigator/menu.rb, line 95
    def render = [tag.prefix, tag.content, items.compact.join, tag.suffix].compact.join

    private

    attr_accessor :template, :tag, :menu_activator, :items

    def respond_to_missing?(name, include_private = false) = ALLOWED_ELEMENTS.match?(name) || super

    def activate_item_attributes! attributes, url, activator
      return unless url == activator.search_value

      key = [attributes[activator.target_key], activator.target_value].compact.join " "
      attributes[activator.target_key] = key
    end
  end
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/navigator/menu.rb, line 101
  def respond_to_missing?(name, include_private = false) = ALLOWED_ELEMENTS.match?(name) || super

  def activate_item_attributes! attributes, url, activator
    return unless url == activator.search_value

    key = [attributes[activator.target_key], activator.target_value].compact.join " "
    attributes[activator.target_key] = key
  end
end