Class: Ekylibre::Navigation::Tree

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(levels) ⇒ Tree

Returns a new instance of Tree.


26
27
28
29
30
# File 'lib/ekylibre/navigation/tree.rb', line 26

def initialize(levels)
  @levels = levels.map(&:to_sym)
  @children = []
  @reversions = {}
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


24
25
26
# File 'lib/ekylibre/navigation/tree.rb', line 24

def children
  @children
end

#indexObject (readonly)

Returns the value of attribute index


24
25
26
# File 'lib/ekylibre/navigation/tree.rb', line 24

def index
  @index
end

#levelsObject (readonly)

Returns the value of attribute levels


24
25
26
# File 'lib/ekylibre/navigation/tree.rb', line 24

def levels
  @levels
end

Class Method Details

.load_file(file, root, levels) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ekylibre/navigation/tree.rb', line 5

def load_file(file, root, levels)
  # Parse XML file
  f = File.open(file)
  doc = Nokogiri::XML(f) do |config|
    config.strict.nonet.noblanks
  end
  f.close

  # Collect levels
  tree = Tree.new(levels)

  doc.xpath("/#{root}/#{levels.first}").each do |element|
    tree.add_child Node.browse_element(element, levels)
  end
  tree.rebuild_index!
  tree
end

Instance Method Details

#[](value) ⇒ Object


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

def [](value)
  @index[value]
end

#add_child(node) ⇒ Object


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

def add_child(node)
  unless node.type == levels.first
    raise "Invalid node type: #{node.type.inspect}. Expecting #{levels.first.inspect}"
  end
  @children << node
end

#get(*keys) ⇒ Object


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

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

#inspectObject


64
65
66
# File 'lib/ekylibre/navigation/tree.rb', line 64

def inspect
  "<#{self.class.name} #{levels.inspect}>"
end

#rebuild_index!Object


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

def rebuild_index!
  @index = {}.with_indifferent_access
  @reversions = {}.with_indifferent_access
  @children.each do |child|
    child.rebuild_index!
    @reversions.deep_merge! child.reversions
    @index[child.name] = child
  end
end

#reverse(controller, action) ⇒ Object


59
60
61
62
# File 'lib/ekylibre/navigation/tree.rb', line 59

def reverse(controller, action)
  return @reversions[controller][action] if @reversions[controller]
  nil
end