Skip to content
Browse files

Added Rack::Bug panel

  • Loading branch information...
1 parent f455ae0 commit 50ec6b7a4a13901f1d87f7097ab69a006ce522f7 @defunkt defunkt committed
View
1 HISTORY.md
@@ -2,6 +2,7 @@
* Unclosed sections now throw a helpful error message
* Report line numbers on unclosed section errors
+* Added Rack::Bug panel
## 0.3.2 (2009-10-19)
View
16 README.md
@@ -364,6 +364,21 @@ An example Sinatra application is also provided:
<http://github.com/defunkt/mustache-sinatra-example>
+[Rack::Bug][4]
+---------
+
+Mustache also ships with a `Rack::Bug` panel. In your `config.ru` add
+the following code:
+
+ require 'rack/bug/panels/mustache_panel'
+ use Rack::Bug::MustachePanel
+
+Using Rails? Add this to your initializer or environment file:
+
+ require 'rack/bug/panels/mustache_panel'
+ config.middleware.use "Rack::Bug::MustachePanel"
+
+
Vim
---
@@ -405,3 +420,4 @@ Meta
[1]: http://code.google.com/p/google-ctemplate/
[2]: http://www.ivan.fomichev.name/2008/05/erlang-template-engine-prototype.html
[3]: http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html
+[4]: http://github.com/brynary/rack-bug/
View
57 lib/rack/bug/panels/mustache_panel.rb
@@ -0,0 +1,57 @@
+module Rack
+ module Bug
+ class MustachePanel < Panel
+ require "rack/bug/panels/mustache_panel/mustache_extension"
+
+ class View < Mustache
+ self.path = ::File.dirname(__FILE__) + '/mustache_panel'
+
+ def times
+ MustachePanel.times.map do |key, value|
+ { :key => key, :value => value }
+ end
+ end
+
+ def variables
+ MustachePanel.variables.map do |key, value|
+ if value.is_a?(Array) && value.size > 10
+ size = value.size
+ value = value.first(10)
+ value << "...and #{size - 10} more"
+ end
+ { :key => key, :value => value.inspect }
+ end
+ end
+ end
+
+ def self.reset
+ Thread.current["rack.bug.mustache.times"] = {}
+ Thread.current["rack.bug.mustache.vars"] = {}
+ end
+
+ def self.times
+ Thread.current["rack.bug.mustache.times"] ||= {}
+ end
+
+ def self.variables
+ Thread.current["rack.bug.mustache.vars"] ||= {}
+ end
+
+ def name
+ "mustache"
+ end
+
+ def heading
+ "{{%.2fms}}" % self.class.times.values.inject(0.0) do |sum, obj|
+ sum + obj
+ end
+ end
+
+ def content
+ View.render
+ ensure
+ self.class.reset
+ end
+ end
+ end
+end
View
22 lib/rack/bug/panels/mustache_panel/mustache_extension.rb
@@ -0,0 +1,22 @@
+if defined? Mustache
+ Mustache.class_eval do
+ alias_method :real_render, :render
+
+ def render(*args, &block)
+ out = ''
+ Rack::Bug::MustachePanel.times[self.class.name] = Benchmark.realtime do
+ out = real_render(*args, &block)
+ end
+ out
+ end
+ end
+
+ Mustache::Context.class_eval do
+ alias_method :real_get, :[]
+
+ def [](name)
+ return real_get(name) if name == :yield || !@mustache.respond_to?(name)
+ Rack::Bug::MustachePanel.variables[name] = real_get(name)
+ end
+ end
+end
View
32 lib/rack/bug/panels/mustache_panel/view.html
@@ -0,0 +1,32 @@
+<h3>Render Times</h3>
+
+<table>
+ <tr>
+ <th>View</th>
+ <th>Render Time</th>
+ </tr>
+
+ {{# times }}
+ <tr>
+ <td>{{ key }}</td>
+ <td>{{ value }}</td>
+ </tr>
+ {{/ times }}
+</table>
+
+<h3>Variables</h3>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Value</th>
+ </tr>
+
+ {{# variables }}
+ <tr>
+ <td>{{ key }}</td>
+ <td>{{ value }}</td>
+ </tr>
+ {{/ variables }}
+</table>
+

0 comments on commit 50ec6b7

Please sign in to comment.
Something went wrong with that request. Please try again.