class DerailedBenchmarks::RequireTree

Constants

REQUIRED_BY

Attributes

cost[W]
name[R]
parent[RW]

Public Class Methods

new(name) click to toggle source
# File lib/derailed_benchmarks/require_tree.rb, line 13
def initialize(name)
  @name     = name
  @children = {}
end

Public Instance Methods

<<(tree) click to toggle source
# 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) click to toggle source
# File lib/derailed_benchmarks/require_tree.rb, line 24
def [](name)
  @children[name.to_s]
end
children() click to toggle source

Returns array of child nodes

# File lib/derailed_benchmarks/require_tree.rb, line 29
def children
  @children.values
end
cost() click to toggle source
# File lib/derailed_benchmarks/require_tree.rb, line 33
def cost
  @cost || 0
end
print_sorted_children(level = 0, out = STDOUT) click to toggle source

Recursively prints all child nodes

sorted_children() click to toggle source

Returns sorted array of child nodes from Largest to Smallest

# File lib/derailed_benchmarks/require_tree.rb, line 38
def sorted_children
  children.sort { |c1, c2| c2.cost <=> c1.cost }
end
to_string() click to toggle source
# 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