Permalink
Browse files

added nokogiri templates and a benchmark controller

  • Loading branch information...
1 parent ecc595e commit a402a87aa96664df92fed4295571c0cffc4e59b2 @mattetti committed Feb 21, 2010
View
33 app/controllers/benchmarks_controller.rb
@@ -0,0 +1,33 @@
+require 'nbuilder'
+class BenchmarksController < ApplicationController
+
+ def noko
+ @leaderboard = mock_leaderboard
+ render :template => 'benchmarks/noko.nxml'
+ end
+
+ def builder
+ @leaderboard = mock_leaderboard
+ render :template => 'benchmarks/builder.rxml'
+ end
+
+ Leaderboard = Struct.new(:total_entries, :row_start, :row_end, :current_page, :total_pages, :columns, :stats )
+
+ class Stat
+ attr_reader :attributes
+ def initialize
+ # fake attributes
+ @attributes = {}
+ 0.upto(15).map{|n| @attributes["attr_#{n}"] = "level='level #{n}', timestamp='#{Time.now}'"}
+ end
+ end
+
+ private
+
+ def mock_leaderboard
+ prep_columns = %w{test0 test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11 test12 test13 test14 test15}
+ Leaderboard.new(1000, 1, 4, 25, 52, prep_columns, 0.upto(50).map{ Stat.new})
+ end
+
+
+end
View
18 app/views/benchmarks/builder.rxml
@@ -0,0 +1,18 @@
+xml.leaderboard :total => @leaderboard.total_entries,
+ :row_start => @leaderboard.row_start,
+ :row_end => @leaderboard.row_end,
+ :page => @leaderboard.current_page,
+ :total_pages => @leaderboard.total_pages,
+ :type => 123,
+ :game_type => 'test' do
+ @leaderboard.stats.each do |leaderboard_stats|
+ xml.player leaderboard_stats.attributes
+ end
+
+ xml.columns do
+ @leaderboard.columns.each do |column|
+ xml.column :name => column, :display_name => column
+ end
+ end
+
+end
View
18 app/views/benchmarks/noko.nxml
@@ -0,0 +1,18 @@
+xml.leaderboard :total => @leaderboard.total_entries,
+ :row_start => @leaderboard.row_start,
+ :row_end => @leaderboard.row_end,
+ :page => @leaderboard.current_page,
+ :total_pages => @leaderboard.total_pages,
+ :type => 123,
+ :game_type => 'test' do
+ @leaderboard.stats.each do |leaderboard_stats|
+ xml.player leaderboard_stats.attributes
+ end
+
+ xml.columns do
+ @leaderboard.columns.each do |column|
+ xml.column :name => column, :display_name => column
+ end
+ end
+
+end
View
2 config/routes.rb
@@ -33,7 +33,7 @@
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
- # See how all your routes lay out with "rake routes"
+ # See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
View
37 lib/nbuilder.rb
@@ -0,0 +1,37 @@
+require 'nokogiri'
+require 'action_view'
+
+class NokogiriBuilder < ActionView::TemplateHandler
+ include ActionView::TemplateHandlers::Compilable
+
+ def compile template
+ "_set_controller_content_type(Mime::XML);" +
+ "xml = ::Nokogiri::XML::Builder.new { |xml|" +
+ template.source +
+ "}.to_xml; self.output_buffer = xml;"
+ end
+end
+
+ActionView::Template.register_template_handler :nxml, NokogiriBuilder
+
+# Usage:
+#
+# index.nxml
+#
+# xml.root do
+# xml.foo do
+# xml.bar
+# end
+# end
+#
+# testy_controller.rb
+#
+#
+# class TestyController < ApplicationController
+# def index
+# respond_to do |format|
+# format.xml
+# end
+# end
+# end
+#
View
3,593 log/development.log
3,593 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit a402a87

Please sign in to comment.