Class: NavNode::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/nav_node/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



5
6
7
# File 'lib/nav_node/node.rb', line 5

def initialize
  @list = []
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



3
4
5
# File 'lib/nav_node/node.rb', line 3

def list
  @list
end

Instance Method Details

#add(content, path = nil, options = {}) ⇒ Object Also known as: +



9
10
11
12
13
14
15
# File 'lib/nav_node/node.rb', line 9

def add(content, path = nil, options = {})
  if !path.nil? || path != ""
    @list << { type: :link, content: content, path: path, options: options }
  else
    @list << {content: content, options: options}
  end
end

#parse_list(request_path, root_path = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nav_node/node.rb', line 18

def parse_list(request_path, root_path = nil)
  nodes = []
  list.each_with_index do |value, index|
    node_match = value[:options][:match]
    node_type = value[:type]
    node_path = value[:path]
    node_class = []
    node_content = ""
    node_class<< "first" if index == 0 
    node_class << "last" if index == list.length - 1
    if url_match(node_match, request_path, node_type, node_path)
      node_class << "active"
    end
    if node_type == :link
      link_class = " class='#{value[:class]}'" if value[:class]
      nodes << "<li class='#{node_class.join(" ")}'><a href='#{node_path}'#{link_class}>#{value[:content]}</a></li>"
    else
      nodes << "<li class='#{node_class.join(" ")}'>#{value[:content]}</li>"
    end
  end
  nodes 
end

#url_match(node_match, request_path, node_type, node_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nav_node/node.rb', line 41

def url_match(node_match, request_path, node_type, node_path)
  match = false
  if node_match
    if node_match.is_a?(String)
      if request_path.match(%r"#{node_match}")
        match = true
      end
    end
    if node_match.is_a?(Array)
      node_match.each do |nm|
        if request_path.match(%r"#{nm}")
          match = true
          break
        end
      end
    end
  else
    if node_type == :link
      if request_path == node_path
        match = true
      end
    end
  end
  return match
end