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

Returns a new instance of Node.


21
22
23
24
25
26
27
28
29
30
# File 'lib/ekylibre/navigation/node.rb', line 21

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def children
  @children
end

#default_pageObject (readonly)

Returns the value of attribute default_page


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def default_page
  @default_page
end

#iconObject (readonly)

Returns the value of attribute icon


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def icon
  @icon
end

#indexObject (readonly)

Returns the value of attribute index


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def name
  @name
end

#pagesObject (readonly)

Returns the value of attribute pages


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def pages
  @pages
end

#parentObject (readonly)

Returns the value of attribute parent


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type


19
20
21
# File 'lib/ekylibre/navigation/node.rb', line 19

def type
  @type
end

Class Method Details

.browse_element(element, levels) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
# 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
  if levels[1]
    element.xpath((levels[1]).to_s).each do |item|
      node.add_child(browse_element(item, levels[1..-1]))
    end
  end
  node
end

Instance Method Details

#add_child(node) ⇒ Object


32
33
34
35
# File 'lib/ekylibre/navigation/node.rb', line 32

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


39
40
41
42
43
# File 'lib/ekylibre/navigation/node.rb', line 39

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


45
46
47
48
49
# File 'lib/ekylibre/navigation/node.rb', line 45

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

#human_nameObject


75
76
77
78
79
80
# File 'lib/ekylibre/navigation/node.rb', line 75

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


82
83
84
# File 'lib/ekylibre/navigation/node.rb', line 82

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

#rebuild_index!Object


51
52
53
54
55
56
57
58
59
60
# File 'lib/ekylibre/navigation/node.rb', line 51

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


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ekylibre/navigation/node.rb', line 62

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