Class: SimplerNavigation::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/simpler_navigation/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, name:, url:, level: 0, options: {}) ⇒ Item

Returns a new instance of Item.



9
10
11
12
13
14
15
16
# File 'lib/simpler_navigation/item.rb', line 9

def initialize(key:, name:, url:, level: 0, options: {})
  @key = key
  @name = name
  @url = url
  @level = level
  @options = options
  @children = {}
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/simpler_navigation/item.rb', line 7

def children
  @children
end

#levelObject (readonly)

Returns the value of attribute level.



5
6
7
# File 'lib/simpler_navigation/item.rb', line 5

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/simpler_navigation/item.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/simpler_navigation/item.rb', line 6

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/simpler_navigation/item.rb', line 4

def url
  @url
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
# File 'lib/simpler_navigation/item.rb', line 30

def [](key)
  @children[key]
end

#fetch(key) ⇒ Object



34
35
36
# File 'lib/simpler_navigation/item.rb', line 34

def fetch(key)
  @children.fetch(key)
end

#item(key, name, url, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simpler_navigation/item.rb', line 18

def item(key, name, url, options = {})
  if @children[key]
    raise "Navigation item already exists for #{key.inspect}"
  end

  @children[key] = Item.new(key: key, name: name, url: url, level: @level + 1, options: options)

  if block_given?
    yield @children[key]
  end
end