Sha256: 0116c04395f6aae8049cdfba853b1627cd0c9fca3c7a4aece661442f21e2841c

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 KB

Contents

require File.dirname(__FILE__) + '/../lib/haml/template'
require 'rubygems'
require 'active_support'
require 'action_view'
require 'profiler'
require 'stringio'

module Haml
  # A profiler for HAML, mostly for development use. This simply implements
  # the Ruby profiler for profiling HAML code.
  class Profiler
  
    # Creates a new profiler that looks for templates in the base
    # directory.
    def initialize(base = File.join(File.dirname(__FILE__), 'templates'))
      ActionView::Base.register_template_handler("haml", Haml::Template)
      unless base.class == ActionView::Base
        @base = ActionView::Base.new(base)
      else
        @base = base
      end
    end

    # Profiles HAML on the given template with the given number of runs.
    # The template name shouldn't have a file extension; this will
    # automatically look for a HAML template.
    # 
    # Returns the results of the profiling as a string.
    def profile(runs = 100, template_name = 'standard')      
      # Runs the profiler, collects information
      Profiler__::start_profile
      runs.times { @base.render template_name }
      Profiler__::stop_profile
      
      # Outputs information to a StringIO, returns result
      io = StringIO.new
      Profiler__::print_profile(io)
      io.pos = 0
      result = io.read
      io.close
      return result
    end

  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
haml-1.0.1 test/profile.rb
haml-1.0.3 test/profile.rb
haml-1.0.0 test/profile.rb
haml-1.0.5 test/profile.rb
haml-1.0.4 test/profile.rb
haml-1.0.2 test/profile.rb
typo-4.1.1 vendor/plugins/haml/test/profile.rb
typo-5.0.2 vendor/plugins/haml/test/profile.rb
typo-4.1 vendor/plugins/haml/test/profile.rb
typo-5.0.1 vendor/plugins/haml/test/profile.rb
typo-5.0 vendor/plugins/haml/test/profile.rb