Class: Ekylibre::Navigation::Node

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, options = {}) ⇒ Node


19
20
21
22
23
24
25
26
27
# File 'lib/ekylibre/navigation/node.rb', line 19

def initialize(type, name, options = {})
  @type = type.to_sym
  @name = name.to_sym
  @icon = options[:icon] || @name
  @children = []
  @pages = []
  @default_page = nil
  @parent = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def children
  @children
end

#default_pageObject (readonly)

Returns the value of attribute default_page


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def default_page
  @default_page
end

#iconObject (readonly)

Returns the value of attribute icon


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def icon
  @icon
end

#indexObject (readonly)

Returns the value of attribute index


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def name
  @name
end

#pagesObject (readonly)

Returns the value of attribute pages


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def pages
  @pages
end

#parentObject (readonly)

Returns the value of attribute parent


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type


17
18
19
# File 'lib/ekylibre/navigation/node.rb', line 17

def type
  @type
end

Class Method Details

.browse_element(element, levels) ⇒ Object


5
6
7
8
9
10
11
12
13
14
# File 'lib/ekylibre/navigation/node.rb', line 5

def browse_element(element, levels)
  node = new(levels[0], element.attr('name').to_s.to_sym, icon: element.attr('icon'))
  element.xpath('page').each do |page|
    node.add_page(page.attr('to'), default: (page.attr('default').to_s == 'true'))
  end
  element.xpath((levels[1]).to_s).each do |item|
    node.add_child(browse_element(item, levels[1..-1]))
  end if levels[1]
  node
end

Instance Method Details

#add_child(node) ⇒ Object


29
30
31
32
# File 'lib/ekylibre/navigation/node.rb', line 29

def add_child(node)
  node.instance_variable_set('@parent', self)
  @children << node
end

#add_page(to, options = {}) ⇒ Object

Add page to node An page can be attached only once by node level


36
37
38
39
40
# File 'lib/ekylibre/navigation/node.rb', line 36

def add_page(to, options = {})
  page = (to.is_a?(Page) ? to : Page.new(to))
  @pages << page unless @pages.include?(page)
  @default_page = page if options[:default] || @pages.size == 1
end

#get(*keys) ⇒ Object


42
43
44
45
46
# File 'lib/ekylibre/navigation/node.rb', line 42

def get(*keys)
  key = keys.shift
  return @index[key].get(keys) if keys.any?
  @index[key]
end

#human_nameObject


72
73
74
75
76
77
# File 'lib/ekylibre/navigation/node.rb', line 72

def human_name
  default = ["navigation.#{@name}".to_sym]
  default << "labels.#{@name}".to_sym if @children.any?
  default << default_page.human_name if default_page
  "navigation.#{@name}_#{@type}".t(default: default)
end

#inspectObject


79
80
81
# File 'lib/ekylibre/navigation/node.rb', line 79

def inspect
  "<#{self.class.name}/#{@type} #{@name}>"
end

#rebuild_index!Object


48
49
50
51
52
53
54
55
56
57
# File 'lib/ekylibre/navigation/node.rb', line 48

def rebuild_index!
  @index = {}.with_indifferent_access
  @children.each do |child|
    child.rebuild_index!
    @index[child.name] = child
    child.pages.each do |page|
      add_page(page) unless @pages.include?(page)
    end
  end
end

#reversionsObject


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ekylibre/navigation/node.rb', line 59

def reversions
  hash = {}.with_indifferent_access
  @children.each do |child|
    hash.deep_merge!(child.reversions)
  end
  @pages.each do |page|
    hash[page.controller] ||= {}.with_indifferent_access
    hash[page.controller][page.action] ||= {}
    hash[page.controller][page.action][@type] = self
  end
  hash
end