class PartialMenu::MenuItem

Represents menu item in a menu

Attributes

id[R]
options[R]
parent[R]
submenu[R]
title[R]
type[R]
uri[R]

Public Class Methods

new(props, parent) click to toggle source

Contructor which check parameter types and start setup

@param [Hash] props The menu list propertiess from the yaml file. @param [PartialMenu::Menu] parent The parent Menu of the item

Throws ArgumentError if parameters are not expected type

# File lib/partial_menu/menu_item.rb, line 22
def initialize(props, parent)
  unless props.is_a? Hash
    raise ::ArgumentError, "Expected Hash, got #{prop.class}"
  end
  unless parent.is_a? PartialMenu::Menu
    raise ::ArgumentError, "Expected PartialMenu::Menu, got #{parent.class}"
  end
  @parent = parent
  @props = props
  setup
end

Public Instance Methods

active?(view) click to toggle source

True if current url relates to the menu item's uri

@params [ActionView] Needs view context to get current request details

@retrun [boolean]

# File lib/partial_menu/menu_item.rb, line 59
def active?(view)
  view.current_page? view.url_for @uri
end
separator?() click to toggle source

True if menu item is a separator

@return [boolean]

# File lib/partial_menu/menu_item.rb, line 48
def separator?
  @type == :separator
end
submenu?() click to toggle source

True if menu item has submenu

@return [boolean]

Private Instance Methods

set_first_entry() click to toggle source

Gets and first entry in the yaml file for this particular menu item as it could hold esential information about the entry

# File lib/partial_menu/menu_item.rb, line 106
def set_first_entry
  @_first_entry = @props.shift
end
set_id() click to toggle source

Sets the menu item id to the entry's name in the yaml

# File lib/partial_menu/menu_item.rb, line 125
def set_id
  @id = @_first_entry[0].to_s
end
set_other_attr() click to toggle source

Parse additional attributes from yaml

If a menu entry defines any addtional attributes above the list below, it will be parsed to an instance variable of the MenuItem.

Built-in properties:

  • :id

  • :uri

  • :title

  • :menu

# File lib/partial_menu/menu_item.rb, line 149
def set_other_attr
  @props.except(:id, :uri, :title, :menu).each do |name, value|
    instance_variable_set('@' + name.to_s, value)
    define_singleton_method(name) { instance_variable_get("@#{name}") }
  end
end
set_submenu() click to toggle source

Check if there is a :menu property for the menu item and intantiate a new PartialMenu::Menu to parse it's content.

# File lib/partial_menu/menu_item.rb, line 82
def set_submenu
  @submenu = nil
  return unless @props.key?(:menu)
  @submenu = PartialMenu::Menu.new(
    @props[:menu],
    @parent.layout,
    self
  )
end
set_title() click to toggle source

Set the title of the menu item based ont the title property from the yaml file or if that not exists, use the yaml entry's name titlelized. If menu item is a separator, :title will be an empty string.

# File lib/partial_menu/menu_item.rb, line 115
def set_title
  @title = ''
  return if @type == :separator
  @title = @props[:title] if @props.key?(:title)
  @title = @id.titleize if @title.blank?
end
set_type() click to toggle source

Sets type property

  • :separator - if menu item is has separator entry only

  • :link - any other occasion

# File lib/partial_menu/menu_item.rb, line 97
def set_type
  @type = :item
  @type = @_first_entry[1].to_sym if @_first_entry[1] == 'separator'
end
set_uri() click to toggle source

Set the URL of the menu item if it is not a separator.

# File lib/partial_menu/menu_item.rb, line 132
def set_uri
  @uri = nil
  @uri = @props[:uri] if @type != :separator
end
setup() click to toggle source

Manage parsing order of yaml properties

# File lib/partial_menu/menu_item.rb, line 68
def setup
  set_first_entry
  set_type
  set_id
  set_title
  set_uri
  set_submenu
  set_other_attr
end