Class: DerailedBenchmarks::RequireTree

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

Constant Summary collapse

REQUIRED_BY =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ RequireTree

Returns a new instance of RequireTree.



13
14
15
16
# File 'lib/derailed_benchmarks/require_tree.rb', line 13

def initialize(name)
  @name     = name
  @children = {}
end

Instance Attribute Details

#costObject



33
34
35
# File 'lib/derailed_benchmarks/require_tree.rb', line 33

def cost
  @cost || 0
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/derailed_benchmarks/require_tree.rb', line 9

def name
  @name
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/derailed_benchmarks/require_tree.rb', line 11

def parent
  @parent
end

Instance Method Details

#<<(tree) ⇒ Object



18
19
20
21
22
# File 'lib/derailed_benchmarks/require_tree.rb', line 18

def <<(tree)
  @children[tree.name.to_s] = tree
  tree.parent = self
  (REQUIRED_BY[tree.name.to_s] ||= []) << self.name
end

#[](name) ⇒ Object



24
25
26
# File 'lib/derailed_benchmarks/require_tree.rb', line 24

def [](name)
  @children[name.to_s]
end

#childrenObject

Returns array of child nodes



29
30
31
# File 'lib/derailed_benchmarks/require_tree.rb', line 29

def children
  @children.values
end

Recursively prints all child nodes



56
57
58
59
60
61
62
63
# File 'lib/derailed_benchmarks/require_tree.rb', line 56

def print_sorted_children(level = 0, out = STDOUT)
  return if cost < ENV['CUT_OFF'].to_f
  out.puts "  " * level + self.to_string
  level += 1
  sorted_children.each do |child|
    child.print_sorted_children(level, out)
  end
end

#sorted_childrenObject

Returns sorted array of child nodes from Largest to Smallest



38
39
40
# File 'lib/derailed_benchmarks/require_tree.rb', line 38

def sorted_children
  children.sort { |c1, c2| c2.cost <=> c1.cost }
end

#to_stringObject



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

def to_string
  str = +"#{name}: #{cost.round(4)} MiB"
  if parent && REQUIRED_BY[self.name.to_s]
    names = REQUIRED_BY[self.name.to_s].uniq - [parent.name.to_s]
    if names.any?
      str << " (Also required by: #{ names.first(2).join(", ") }"
      str << ", and #{names.count - 2} others" if names.count > 3
      str << ")"
    end
  end
  str
end