Class: PartialMenu::Menu

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

Overview

Represents a menu with menu items

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props, layout = 'main', parent = nil) ⇒ Menu

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/partial_menu/menu.rb', line 12

def initialize(props, layout = 'main', parent = nil)
  unless props.is_a? Array
    raise ::ArgumentError, "Expected an Array, got #{props.class}"
  end
  unless layout.is_a? String
    raise ::ArgumentError, "Expected a String, got #{layout.class}"
  end
  unless parent.nil? || (parent.is_a? PartialMenu::MenuItem)
    raise ::ArgumentError, "Expected MenuItem or nil, got #{parent.class}"
  end
  @props = props
  @layout = layout
  @items = []
  @parent = parent
  set_id
  add_items(props)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/partial_menu/menu.rb', line 9

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/partial_menu/menu.rb', line 7

def items
  @items
end

#layoutObject (readonly)

Returns the value of attribute layout.



6
7
8
# File 'lib/partial_menu/menu.rb', line 6

def layout
  @layout
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/partial_menu/menu.rb', line 8

def parent
  @parent
end

Instance Method Details

#active?(view) ⇒ Boolean

True if current url relates to any child menu item’s uri

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/partial_menu/menu.rb', line 38

def active?(view)
  @items.each do |item|
    return true if item.active?(view)
  end
  false
end