summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2010-12-03 11:25:21 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2010-12-03 11:25:21 +0000
commit96ce0f017cfe58210d84e5092446395b811ccdab (patch)
tree9db6a729161c9d10377e8cb3bb1509b47b588943 /lib
parent483133285e96ad5701d0ce11d76a106d8c5e49cd (diff)
downloadredmine-96ce0f017cfe58210d84e5092446395b811ccdab.tar.gz
redmine-96ce0f017cfe58210d84e5092446395b811ccdab.zip
Adds a builder-like template system for rendering xml and json API responses.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4452 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/redmine.rb2
-rw-r--r--lib/redmine/views/api_template_handler.rb28
-rw-r--r--lib/redmine/views/builders.rb35
-rw-r--r--lib/redmine/views/builders/json.rb30
-rw-r--r--lib/redmine/views/builders/structure.rb68
-rw-r--r--lib/redmine/views/builders/xml.rb45
6 files changed, 208 insertions, 0 deletions
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 1e6a71810..2b01f25e8 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -229,3 +229,5 @@ end
Redmine::WikiFormatting.map do |format|
format.register :textile, Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting::Textile::Helper
end
+
+ActionView::Template.register_template_handler :apit, Redmine::Views::ApiTemplateHandler
diff --git a/lib/redmine/views/api_template_handler.rb b/lib/redmine/views/api_template_handler.rb
new file mode 100644
index 000000000..4f73227ec
--- /dev/null
+++ b/lib/redmine/views/api_template_handler.rb
@@ -0,0 +1,28 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module Views
+ class ApiTemplateHandler < ActionView::TemplateHandler
+ include ActionView::TemplateHandlers::Compilable
+
+ def compile(template)
+ "Redmine::Views::Builders.for(params[:format]) do |api|; #{template.source}; self.output_buffer = api.output; end"
+ end
+ end
+ end
+end
diff --git a/lib/redmine/views/builders.rb b/lib/redmine/views/builders.rb
new file mode 100644
index 000000000..6e0761c2f
--- /dev/null
+++ b/lib/redmine/views/builders.rb
@@ -0,0 +1,35 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module Views
+ module Builders
+ def self.for(format, &block)
+ builder = case format
+ when 'xml', :xml; Builders::Xml.new
+ when 'json', :json; Builders::Json.new
+ else; raise "No builder for format #{format}"
+ end
+ if block
+ block.call(builder)
+ else
+ builder
+ end
+ end
+ end
+ end
+end
diff --git a/lib/redmine/views/builders/json.rb b/lib/redmine/views/builders/json.rb
new file mode 100644
index 000000000..61037eb30
--- /dev/null
+++ b/lib/redmine/views/builders/json.rb
@@ -0,0 +1,30 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+require 'blankslate'
+
+module Redmine
+ module Views
+ module Builders
+ class Json < Structure
+ def output
+ @struct.first.to_json
+ end
+ end
+ end
+ end
+end
diff --git a/lib/redmine/views/builders/structure.rb b/lib/redmine/views/builders/structure.rb
new file mode 100644
index 000000000..b1add8be8
--- /dev/null
+++ b/lib/redmine/views/builders/structure.rb
@@ -0,0 +1,68 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+require 'blankslate'
+
+module Redmine
+ module Views
+ module Builders
+ class Structure < BlankSlate
+ def initialize
+ @struct = [{}]
+ end
+
+ def array(tag, &block)
+ @struct << []
+ block.call(self)
+ ret = @struct.pop
+ @struct.last[tag] = ret
+ end
+
+ def method_missing(sym, *args, &block)
+ if args.any?
+ if args.first.is_a?(Hash)
+ if @struct.last.is_a?(Array)
+ @struct.last << args.first
+ end
+ else
+ if @struct.last.is_a?(Array)
+ @struct.last << (args.last || {}).merge(:value => args.first)
+ else
+ @struct.last[sym] = args.first
+ end
+ end
+ end
+
+ if block
+ @struct << {}
+ block.call(self)
+ ret = @struct.pop
+ if @struct.last.is_a?(Array)
+ @struct.last << ret
+ else
+ @struct.last[sym] = ret
+ end
+ end
+ end
+
+ def output
+ raise "Need to implement #{self.class.name}#output"
+ end
+ end
+ end
+ end
+end
diff --git a/lib/redmine/views/builders/xml.rb b/lib/redmine/views/builders/xml.rb
new file mode 100644
index 000000000..41a767154
--- /dev/null
+++ b/lib/redmine/views/builders/xml.rb
@@ -0,0 +1,45 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module Views
+ module Builders
+ class Xml < ::Builder::XmlMarkup
+ def initialize
+ super
+ instruct!
+ end
+
+ def output
+ target!
+ end
+
+ def method_missing(sym, *args, &block)
+ if args.size == 1 && args.first.is_a?(Time)
+ __send__ sym, args.first.xmlschema, &block
+ else
+ super
+ end
+ end
+
+ def array(name, options={}, &block)
+ __send__ name, options.merge(:type => 'array'), &block
+ end
+ end
+ end
+ end
+end