Class: DependencyTree

Inherits:
Object
  • Object
show all
Defined in:
lib/dependency_tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(dependency_map) ⇒ DependencyTree

Returns a new instance of DependencyTree.



25
26
27
28
# File 'lib/dependency_tree.rb', line 25

def initialize dependency_map
  @dependency_map = dependency_map
  @root_node = nil
end

Instance Method Details

#next_node(current) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dependency_tree.rb', line 43

def next_node current
  return nil if current.to_s.strip.length == 0
  return nil if @dependency_map.nil? || !@dependency_map.is_a?(GlobalConstants::HASH) || @dependency_map.empty?

  if @dependency_map[current].has_key? GlobalConstants::NEXT
    next_node_name = @dependency_map[current][GlobalConstants::NEXT]
    next_node = @dependency_map[next_node_name]
    return nil if next_node.nil?
    return Hashit.new next_node, next_node_name
  end
end

#previous_node(current) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dependency_tree.rb', line 55

def previous_node current
  return nil if current.to_s.strip.length == 0
  return nil if @dependency_map.nil? || !@dependency_map.is_a?(GlobalConstants::HASH) || @dependency_map.empty?

  if @dependency_map[current].has_key? GlobalConstants::PREVIOUS
    prev_node_name = @dependency_map[current][GlobalConstants::PREVIOUS]
    prev_node = @dependency_map[prev_node_name]
    return nil if prev_node.nil?
    return Hashit.new prev_node, prev_node_name
  end
end

#rootObject



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

def root
  return nil if @dependency_map.nil? || !@dependency_map.is_a?(GlobalConstants::HASH) || @dependency_map.empty?
  return @root_node if !@root_node.nil?

  @dependency_map.each do |k,v| 
    if v.has_key?(GlobalConstants::IS_ROOT) && v[GlobalConstants::IS_ROOT].downcase == 'y'
      root_node = Hashit.new v, k
      @root_node = root_node
      return @root_node
    end
  end
end

#traverse {|current| ... } ⇒ Object

Yields:

  • (current)


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dependency_tree.rb', line 67

def traverse
  current = root
  yield current
  while current != nil
    begin
      current = next_node current._node_name
    rescue
      #puts $!
      current = nil
    end
    yield current if !current.nil?
  end
end