Class: SemanticNavigation::Core::Navigation

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_navigation/core/navigation.rb

Direct Known Subclasses

Node

Instance Attribute Summary collapse

Attributes inherited from Base

#active, #classes, #html, #id, #level, #render_if

Instance Method Summary collapse

Methods inherited from Base

#render

Constructor Details

#initialize(options, level = 0) ⇒ Navigation

Returns a new instance of Navigation.



6
7
8
9
10
# File 'lib/semantic_navigation/core/navigation.rb', line 6

def initialize(options, level = 0)
  @sub_elements = []
  @scope_options = {}
  super options, level
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/semantic_navigation/core/navigation.rb', line 64

def method_missing(m,*args,&block)
  if m.to_s.match(/^[_]+$/).to_a.size > 0
    divider
  else
    super(m,args,&block)
  end
end

Instance Attribute Details

#sub_elementsObject

Returns the value of attribute sub_elements.



4
5
6
# File 'lib/semantic_navigation/core/navigation.rb', line 4

def sub_elements
  @sub_elements
end

Instance Method Details

#divider(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/semantic_navigation/core/navigation.rb', line 55

def divider(options = {})
  options[:id] = :divider
  options[:render_if] ||= @scope_options[:render_if]
  options[:url] = nil
  options[:i18n_name] = nil
  options[:name] = nil
  @sub_elements.push Leaf.new(options, @level+1)
end

#header(id, options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/semantic_navigation/core/navigation.rb', line 47

def header(id, options={})
  options[:id] = id.to_sym
  options[:render_if] ||= @scope_options[:render_if]
  options[:url] = nil
  options[:i18n_name] = @i18n_name
  @sub_elements.push Leaf.new(options, @level+1)
end

#item(id, url = nil, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_navigation/core/navigation.rb', line 12

def item(id, url=nil, options={}, &block)
  options[:id] = id.to_sym
  options[:render_if] ||= @scope_options[:render_if]

  if url.is_a?(Array)
    options[:url] = [url].flatten(1).map{|url| decode_url(url)}
  else
    options[:url] = decode_url(url)
  end

  options[:i18n_name] = @i18n_name

  if block_given?
    element = Node.new(options, @level+1)
    element.instance_eval &block
  else
    element = Leaf.new(options, @level+1)
    #Deprecation warning message
    #TODO:Should be deleted after moving the header and divider definition via item
    if element.url.nil? && !element.name.empty?
      SemanticNavigation.deprecation_message(:method,
                                             'item',
                                             'header',
                                             'header definition')
    elsif element.url.nil? && element.name.empty?
      SemanticNavigation.deprecation_message(:method,
                                             'item',
                                             'header',
                                             'divider definition')
    end
  end

  @sub_elements.push element
end

#mark_activeObject



78
79
80
81
82
83
# File 'lib/semantic_navigation/core/navigation.rb', line 78

def mark_active
  @sub_elements.each do |element|
    element.mark_active
  end
  @active = !@sub_elements.find{|element| element.active}.nil?
end

#scope(options = {}, &block) ⇒ Object



72
73
74
75
76
# File 'lib/semantic_navigation/core/navigation.rb', line 72

def scope(options = {}, &block)
  @scope_options = options
  self.instance_eval &block
  @scope_options = {}
end