Class: DerailedBenchmarks::RequireTree
- Defined in:
- lib/derailed_benchmarks/require_tree.rb
Constant Summary collapse
- REQUIRED_BY =
{}
Instance Attribute Summary collapse
- #cost ⇒ Object
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(tree) ⇒ Object
- #[](name) ⇒ Object
-
#children ⇒ Object
Returns array of child nodes.
-
#initialize(name) ⇒ RequireTree
constructor
A new instance of RequireTree.
-
#print_sorted_children(level = 0, out = STDOUT) ⇒ Object
Recursively prints all child nodes.
-
#sorted_children ⇒ Object
Returns sorted array of child nodes from Largest to Smallest.
- #to_string ⇒ Object
Constructor Details
#initialize(name) ⇒ RequireTree
Returns a new instance of RequireTree.
13 14 15 16 17 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 13 def initialize(name) @name = name @children = {} @cost = 0 end |
Instance Attribute Details
#cost ⇒ Object
43 44 45 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 43 def cost @cost || 0 end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 9 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
11 12 13 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 11 def parent @parent end |
Class Method Details
.reset! ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 19 def self.reset! REQUIRED_BY.clear if defined?(Kernel::REQUIRE_STACK) Kernel::REQUIRE_STACK.clear Kernel::REQUIRE_STACK.push(TOP_REQUIRE) end end |
Instance Method Details
#<<(tree) ⇒ Object
28 29 30 31 32 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 28 def <<(tree) @children[tree.name.to_s] = tree tree.parent = self (REQUIRED_BY[tree.name.to_s] ||= []) << self.name end |
#[](name) ⇒ Object
34 35 36 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 34 def [](name) @children[name.to_s] end |
#children ⇒ Object
Returns array of child nodes
39 40 41 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 39 def children @children.values end |
#print_sorted_children(level = 0, out = STDOUT) ⇒ Object
Recursively prints all child nodes
66 67 68 69 70 71 72 73 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 66 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_children ⇒ Object
Returns sorted array of child nodes from Largest to Smallest
48 49 50 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 48 def sorted_children children.sort { |c1, c2| c2.cost <=> c1.cost } end |
#to_string ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/derailed_benchmarks/require_tree.rb', line 52 def to_string str = String.new("#{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 |