class PartialMenu::MenuItem
Represents menu item in a menu
Attributes
Public Class Methods
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
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
True if menu item is a separator
@return [boolean]
# File lib/partial_menu/menu_item.rb, line 48 def separator? @type == :separator end
Private Instance Methods
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
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
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 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
Sets type property
-
:separator
- if menu item is hasseparator
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 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
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