Sha256: 5dfd93bb12241fab05e24a14a69842a022df825e5a7b0be0cff7de4a6368f241

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'tree'
module BootPolish
  class TreeRenderer

    def initialize(output = nil)
      @output = output || STDOUT
      @tree = Tree::TreeNode.new("ROOT", "Root Content")
      @current_node = @tree
    end

    def descend
      new_node = Tree::TreeNode.new("node-#{@current_node.level}-#{@current_node.children.count}")
      @current_node << new_node
      @current_node = new_node
    end

    def exception(method, exception)
      @current_node.content = { method: method, exception: exception }
    end

    def benchmark method, time
      @current_node.content = { method: method, time: time }
    end

    def ascend
      render(@current_node) if @current_node.parent.name == "ROOT"
      @current_node = @current_node.parent
    end
    private

    def render(current_node)
      result = current_node.content
      @output << "  " * (current_node.level - 1)
      if result[:exception]
        @output << "#{result[:method]} raised exception: #{result[:exception].message}"
      else
        @output << format("%.4f for #{result[:method]}", result[:time].real)
      end
      @output << "\n"

      current_node.children.each do |child|
        render child
      end
      @output.flush
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
boot_polish-0.0.1 lib/boot_polish/tree_renderer.rb