mschuerig-ruby_template_handler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-04-02
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/ruby_template_handler.rb
6
+ rails/init.rb
7
+ test/test_helper.rb
8
+ test/test_ruby_template_handler.rb
@@ -0,0 +1,77 @@
1
+ = ruby_template_handler
2
+
3
+ * http://github.com/mschuerig/ruby_template_handler
4
+
5
+ == DESCRIPTION:
6
+
7
+ A Rails view template handler that renders Ruby objects to JSON.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * I'm not entirely sure how sensible this whole thing is, but there you go.
12
+
13
+ == SYNOPSIS:
14
+
15
+ A controller action for returning a list of Movie objects in JSON format
16
+
17
+ # app/controllers/movies_controller.rb
18
+ def index
19
+ respond_to do |format|
20
+ format.json do
21
+ @movies = Movie.all
22
+ @count = Movie.count
23
+ render :template => 'movies/index.json.rb'
24
+ end
25
+ end
26
+ end
27
+
28
+ # app/views/movies/index.json.rb
29
+ {
30
+ :identifier => Movie.primary_key,
31
+ :totalCount => @count,
32
+ # render @movies does not work as it insists on returning a string
33
+ :items => @movies.map { |m| render(m) }
34
+ }
35
+
36
+ # app/views/movies/_movie.json.rb
37
+ {
38
+ :id => movie.to_param,
39
+ :title => movie.title,
40
+ :releaseDate => movie.release_date
41
+ }
42
+
43
+ In general, "full" templates are rendered to strings, whereas
44
+ partials are rendered as objects.
45
+
46
+ == REQUIREMENTS:
47
+
48
+ * Rails
49
+
50
+ == INSTALL:
51
+
52
+ * sudo gem install mschuerig-ruby_template_handler
53
+
54
+ == LICENSE:
55
+
56
+ (The MIT License)
57
+
58
+ Copyright (c) 2009 Michael Schuerig
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining
61
+ a copy of this software and associated documentation files (the
62
+ 'Software'), to deal in the Software without restriction, including
63
+ without limitation the rights to use, copy, modify, merge, publish,
64
+ distribute, sublicense, and/or sell copies of the Software, and to
65
+ permit persons to whom the Software is furnished to do so, subject to
66
+ the following conditions:
67
+
68
+ The above copyright notice and this permission notice shall be
69
+ included in all copies or substantial portions of the Software.
70
+
71
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
72
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
73
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
74
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
75
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
76
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
77
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/ruby_template_handler'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('ruby_template_handler', RubyTemplateHandler::VERSION) do |p|
7
+ p.developer('Michael Schuerig', 'michael@schuerig.de')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.extra_deps = [
10
+ ['actionpack','>= 2.2'],
11
+ ]
12
+ p.extra_dev_deps = [
13
+ ['newgem', ">= #{::Newgem::VERSION}"]
14
+ ]
15
+
16
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
17
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
18
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
19
+ p.rsync_args = '-av --delete --ignore-errors'
20
+ end
21
+
22
+ require 'newgem/tasks' # load /tasks/*.rake
23
+ Dir['tasks/**/*.rake'].each { |t| load t }
@@ -0,0 +1,15 @@
1
+
2
+ require 'action_view'
3
+
4
+ class RubyTemplateHandler < ActionView::TemplateHandler
5
+ VERSION = '0.0.1'
6
+ include ActionView::TemplateHandlers::Compilable
7
+
8
+ def compile(template)
9
+ src = 'self.output_buffer = (' + template.source + ')'
10
+ unless File.basename(template.filename).starts_with?('_')
11
+ src += '.to_json'
12
+ end
13
+ src
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ # Include hook code here
2
+ require 'ruby_template_handler'
3
+ ActionView::Template.register_template_handler :rb, RubyTemplateHandler
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/ruby_template_handler'
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'ostruct'
3
+ require 'action_controller'
4
+
5
+ ActionView::Template.register_template_handler :rb, RubyTemplateHandler
6
+
7
+ class Movie
8
+ def self.primary_key
9
+ 'id'
10
+ end
11
+ end
12
+
13
+ class TestRubyTemplateHandler < Test::Unit::TestCase
14
+
15
+ class TestController < ActionController::Base
16
+ def initialize(movies)
17
+ @movies = Array(movies)
18
+ @count = @movies.size
19
+ end
20
+ undef_method :request, :response
21
+ end
22
+
23
+ class TestTemplate < ActionView::Template
24
+ def initialize(filename, source)
25
+ @filename, @source = filename, source
26
+ self.format = 'json'
27
+ self.extension = 'rb'
28
+ end
29
+ def source
30
+ @source
31
+ end
32
+ end
33
+
34
+ def setup
35
+ @movie = OpenStruct.new({
36
+ :title => 'Rendered Insane',
37
+ :release_date => Time.utc(2008, 4, 2)
38
+ })
39
+
40
+ controller = TestController.new(@movie)
41
+ @view = ActionView::Base.new
42
+ @view.controller = controller
43
+
44
+ @template = TestTemplate.new('app/views/movies/index.json.rb', <<TMPL
45
+ {
46
+ :identifier => Movie.primary_key,
47
+ :totalCount => @count
48
+ }
49
+ TMPL
50
+ )
51
+
52
+ @partial = TestTemplate.new('app/views/movies/_movie.json.rb', <<TMPL
53
+ {
54
+ :title => movie.title,
55
+ :releaseDate => movie.release_date
56
+ }
57
+ TMPL
58
+ )
59
+ end
60
+
61
+ def test_render_template
62
+ result = @template.render(@view)
63
+ assert_kind_of String, result
64
+ assert_match /"identifier": "id"/, result
65
+ assert_match /"totalCount": 1/, result
66
+ end
67
+
68
+ def test_render_partial
69
+ result = @partial.render(@view, :movie => @movie)
70
+ assert_kind_of Hash, result
71
+ assert_equal(
72
+ {
73
+ :title => 'Rendered Insane',
74
+ :releaseDate => Time.utc(2008, 4, 2)
75
+ }, result)
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mschuerig-ruby_template_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Schuerig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.2"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: newgem
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.0
44
+ version:
45
+ description: Rails view templates that return Ruby objects.
46
+ email:
47
+ - michael@schuerig.de
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - README.rdoc
56
+ files:
57
+ - History.txt
58
+ - Manifest.txt
59
+ - README.rdoc
60
+ - Rakefile
61
+ - lib/ruby_template_handler.rb
62
+ - rails/init.rb
63
+ - test/test_helper.rb
64
+ - test/test_ruby_template_handler.rb
65
+ has_rdoc: false
66
+ homepage: http://github.com/mschuerig/ruby_template_handler
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --main
70
+ - README.rdoc
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project: ruby_template_handler
88
+ rubygems_version: 1.2.0
89
+ signing_key:
90
+ specification_version: 2
91
+ summary: Rails view templates that return Ruby objects.
92
+ test_files:
93
+ - test/test_helper.rb
94
+ - test/test_ruby_template_handler.rb