Class: MultiRepo::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parent = nil, depth = 0) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
# File 'lib/multirepo/logic/node.rb', line 10

def initialize(path, parent = nil, depth = 0)
  @path = path
  @depth = depth
  @parent = parent
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



7
8
9
# File 'lib/multirepo/logic/node.rb', line 7

def depth
  @depth
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/multirepo/logic/node.rb', line 8

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/multirepo/logic/node.rb', line 6

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
# File 'lib/multirepo/logic/node.rb', line 68

def ==(other)
  other.class == self.class &&
  otherPath = Utils.standard_path(other.path)
  thisPath = Utils.standard_path(@path)
  otherPath.casecmp(thisPath) == 0
end

#childrenObject



20
21
22
23
24
# File 'lib/multirepo/logic/node.rb', line 20

def children
  return [] unless Utils.multirepo_enabled?(@path)
  config_entries = ConfigFile.new(@path).load_entries
  return config_entries.map { |e| Node.new(e.path, self, @depth + 1) }
end

#ensure_no_dependency_cycle(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/multirepo/logic/node.rb', line 50

def ensure_no_dependency_cycle(node)
  parent = node.parent
  visited = []
  while parent
    visited.push(parent)
    if parent == node
      Console.log_warning("Dependency cycle detected:")
      visited.reverse.each_with_index do |n, i|
        description = "[first]" if i == visited.count - 1
        description = "itself" if visited.count == 1
        Console.log_warning("'#{n.path}' depends on #{description}")
      end
      fail MultiRepoException, "Dependency cycles are not supported by multirepo."
    end
    parent = parent.parent # Will eventually be nil (root node), which will break out of the loop
  end
end

#find_descendants_recursive(node) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/multirepo/logic/node.rb', line 42

def find_descendants_recursive(node)
  ensure_no_dependency_cycle(node)
  
  descendants = node.children
  descendants.each { |d| descendants.push(*find_descendants_recursive(d)) }
  return descendants
end

#nameObject



16
17
18
# File 'lib/multirepo/logic/node.rb', line 16

def name
  Pathname.new(File.expand_path(@path)).basename.to_s
end

#ordered_descendantsObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/multirepo/logic/node.rb', line 30

def ordered_descendants
  descendants = find_descendants_recursive(self)
  
  unique_paths = descendants.map(&:path).uniq
  unique_nodes = unique_paths.collect do |path|
    nodes_for_path = descendants.select { |d| d.path == path }
    next nodes_for_path.sort_by(&:depth).first
  end
  
  return unique_nodes.sort_by(&:depth).reverse
end

#ordered_descendants_including_selfObject



26
27
28
# File 'lib/multirepo/logic/node.rb', line 26

def ordered_descendants_including_self
  return ordered_descendants.push(self)
end