lib/gestalt.rb in gestalt-0.0.2 vs lib/gestalt.rb in gestalt-0.0.3

- old
+ new

@@ -3,22 +3,23 @@ require 'rubygems' require 'formatador' class Gestalt - VERSION = '0.0.2' + VERSION = '0.0.3' - attr_accessor :calls, :formatador + attr_accessor :calls - def initialize + def initialize(formatador = Formatador.new) @calls = [] + @formatador = Formatador.new @stack = [] @totals = {} end def display_calls - formatador.display_line + @formatador.display_line condensed = [] total = 0.0 for call in @calls if condensed.last && condensed.last == call condensed.last.durations.concat(call.durations) @@ -26,13 +27,13 @@ condensed << call end total += call.duration end for call in condensed - call.display(total, formatador) + call.display(total, @formatador) end - formatador.display_line + @formatador.display_line end def display_profile for call in calls parse_call(call) @@ -45,19 +46,15 @@ :duration => format("%.6f", value[:duration]) } end table = table.sort {|x,y| y[:duration] <=> x[:duration]} - formatador.display_line - formatador.display_table(table) - formatador.display_line + @formatador.display_line + @formatador.display_table(table) + @formatador.display_line end - def formatador - @formatador ||= Formatador.new - end - def run(&block) Kernel.set_trace_func( lambda do |event, file, line, id, binding, classname| case event when 'call', 'c-call' @@ -95,17 +92,17 @@ @calls << call end end end - def self.profile(&block) - gestalt = new + def self.profile(formatador = Formatador.new, &block) + gestalt = new(formatador) gestalt.run(&block) gestalt.display_profile end - def self.trace(&block) - gestalt = new + def self.trace(formatador = Formatador.new, &block) + gestalt = new(formatador) gestalt.run(&block) gestalt.display_calls end private