simonmenke-render_any 0.0.2

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.
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Simon Menke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,91 @@
1
+ = RenderAny
2
+
3
+ RenderAny allows you to describe how a (restful) controller behaves for a specific format.
4
+
5
+
6
+ == Example
7
+
8
+ This is an example controller which responds to <tt>:html</tt>, <tt>:xml</tt>, <tt>:json</tt> and <tt>:yaml</tt>
9
+
10
+ class PostsController < ApplicationController
11
+ def index
12
+ @posts = Post.all
13
+ render_any @posts
14
+ end
15
+
16
+ def show
17
+ @post = Post.find(params[:id])
18
+ render_any @post
19
+ end
20
+
21
+ def new
22
+ @post = Post.new
23
+ render_any @post
24
+ end
25
+
26
+ def edit
27
+ @post = Post.find(params[:id])
28
+ render_any @post
29
+ end
30
+
31
+ def create
32
+ @post = Post.new(params[:post])
33
+ render_create(@post) { @post.save }
34
+ end
35
+
36
+ def update
37
+ @post = Post.find(params[:id])
38
+ render_update(@post) { @post.update_attributes(params[:post]) }
39
+ end
40
+
41
+ def destroy
42
+ @post = Post.find(params[:id])
43
+ render_destroy(@post) { @post.destroy }
44
+ end
45
+ end
46
+
47
+ == Adding a format
48
+
49
+ Adding a format is easy, just put something like this in an initializer.
50
+
51
+ RenderAny.register :html do |s|
52
+
53
+ s.default do |object, options|
54
+ render
55
+ end
56
+
57
+ s.not_found do |object, options|
58
+ render_optional_error_file :not_found
59
+ end
60
+
61
+ s.unprocessable_entity do |object, options|
62
+ render_optional_error_file :unprocessable_entity
63
+ end
64
+
65
+ s.create do |object, options|
66
+ redirect_to object
67
+ end
68
+
69
+ s.create_failed do |object, options|
70
+ render :action => 'new'
71
+ end
72
+
73
+ s.update do |object, options|
74
+ redirect_to object
75
+ end
76
+
77
+ s.update_failed do |object, options|
78
+ render :action => 'edit'
79
+ end
80
+
81
+ s.destroy do |object, options|
82
+ redirect_to [object.class.to_s.underscore.pluralize.to_sym]
83
+ end
84
+
85
+ s.destroy_failed do |object, options|
86
+ redirect_to object
87
+ end
88
+
89
+ end
90
+
91
+ Copyright (c) 2009 Simon Menke, released under the MIT license
@@ -0,0 +1,42 @@
1
+ module RenderAny
2
+ module ActionController #:nodoc:
3
+ module RenderAny
4
+
5
+ def render_any(object, options={})
6
+ mime_types = ::RenderAny.mime_type_for_request(request)
7
+
8
+ if object.nil?
9
+ render_any_with(:not_found, false, mime_types, object, options)
10
+ else
11
+ render_any_with(params[:action], true, mime_types, object, options)
12
+ end
13
+
14
+ end
15
+
16
+ private
17
+
18
+ def serialize_and_render_with(handler, use_default, mime_types, object, options)
19
+ mime_types.each do |type|
20
+ serializer = ::RenderAny.serializers[type.to_sym]
21
+ next if serializer.nil?
22
+ next if !serializer.has_handler? handler and !use_default
23
+ handler = serializer.handler(handler)
24
+ handler = handler.bind(self)
25
+ handler.call(object, options)
26
+ return true
27
+ end
28
+
29
+ return false
30
+ end
31
+
32
+ def render_any_with(handler, use_default, mime_types, object, options)
33
+ if serialize_and_render_with(handler, use_default, mime_types, object, options)
34
+ return true
35
+ else
36
+ serialize_and_render_with(:unprocessable_entity, false, mime_types, object, options)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ module RenderAny
2
+ module ActionController #:nodoc:
3
+ module RenderAnyFailable
4
+
5
+ def render_any_failable(object, options={}, success=nil, &block)
6
+ mime_types = ::RenderAny.mime_type_for_request(request)
7
+
8
+ if success.nil? and object and block
9
+ success = block.call(object)
10
+ end
11
+
12
+ if object.nil?
13
+ render_any_with(:not_found, false, mime_types, object, options)
14
+ elsif success
15
+ render_any_with(params[:action], true, mime_types, object, options)
16
+ else
17
+ render_any_with("#{params[:action]}_failed", true, mime_types, object, options)
18
+ end
19
+
20
+ end
21
+
22
+ alias_method :render_update, :render_any_failable
23
+ alias_method :render_create, :render_any_failable
24
+ alias_method :render_destroy, :render_any_failable
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ # RenderAny
2
+ module RenderAny
3
+
4
+ class Serializer
5
+ attr_reader :format
6
+
7
+ def initialize(format, &block)
8
+ @format = format.to_sym
9
+ block.call(self)
10
+ end
11
+
12
+ def self.handler(name, fallback=:default)
13
+ class_eval %{
14
+ def #{name}(&block)
15
+ @#{name}_proc = block if block
16
+ @#{name}_proc #{fallback ? "|| #{fallback}" : ''}
17
+ end
18
+ }
19
+ end
20
+
21
+ def self.configure
22
+ yield self
23
+ end
24
+
25
+ def handler(name)
26
+ send(name)
27
+ end
28
+
29
+ def has_handler?(name)
30
+ !! instance_variable_get("@#{name}_proc")
31
+ end
32
+
33
+ end
34
+
35
+ end
data/lib/render_any.rb ADDED
@@ -0,0 +1,21 @@
1
+ # RenderAny
2
+ module RenderAny
3
+
4
+ class << self
5
+ attr_accessor :serializers
6
+ end
7
+ self.serializers = {}
8
+
9
+ def self.register(format, &block)
10
+ self.serializers[format.to_sym] = Serializer.new(format, &block)
11
+ end
12
+
13
+ def self.mime_type_for_request(request)
14
+ if ::ActionController::Base.use_accept_header
15
+ Array(Mime::Type.lookup_by_extension(request.parameters[:format]) || request.accepts)
16
+ else
17
+ [request.format]
18
+ end
19
+ end
20
+
21
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,181 @@
1
+ # Include hook code here
2
+ ::ActionController::Base.send :include, ::RenderAny::ActionController::RenderAny
3
+ ::ActionController::Base.send :include, ::RenderAny::ActionController::RenderAnyFailable
4
+
5
+ ::RenderAny::Serializer.configure do |s|
6
+ s.handler(:default, nil)
7
+ s.handler(:not_found)
8
+ s.handler(:unprocessable_entity)
9
+
10
+ s.handler(:index)
11
+ s.handler(:show)
12
+ s.handler(:new)
13
+ s.handler(:edit)
14
+
15
+ s.handler(:create)
16
+ s.handler(:create_failed, :create)
17
+ s.handler(:update)
18
+ s.handler(:update_failed, :update)
19
+ s.handler(:destroy)
20
+ s.handler(:destroy_failed, :destroy)
21
+ end
22
+
23
+ ::RenderAny.register :html do |s|
24
+
25
+ s.default do |object, options|
26
+ render
27
+ end
28
+
29
+ s.not_found do |object, options|
30
+ render_optional_error_file :not_found
31
+ end
32
+
33
+ s.unprocessable_entity do |object, options|
34
+ render_optional_error_file :unprocessable_entity
35
+ end
36
+
37
+ s.create do |object, options|
38
+ redirect_to object
39
+ end
40
+
41
+ s.create_failed do |object, options|
42
+ render :action => 'new'
43
+ end
44
+
45
+ s.update do |object, options|
46
+ redirect_to object
47
+ end
48
+
49
+ s.update_failed do |object, options|
50
+ render :action => 'edit'
51
+ end
52
+
53
+ s.destroy do |object, options|
54
+ redirect_to [object.class.to_s.underscore.pluralize.to_sym]
55
+ end
56
+
57
+ s.destroy_failed do |object, options|
58
+ redirect_to object
59
+ end
60
+
61
+ end
62
+
63
+ ::RenderAny.register :xml do |s|
64
+
65
+ s.default do |object, options|
66
+ render :xml => object.to_xml(options)
67
+ end
68
+
69
+ s.not_found do |object, options|
70
+ render_optional_error_file :not_found
71
+ end
72
+
73
+ s.unprocessable_entity do |object, options|
74
+ render_optional_error_file :unprocessable_entity
75
+ end
76
+
77
+ s.create do |object, options|
78
+ render :xml => object.to_xml(options), :status => :created, :location => object
79
+ end
80
+
81
+ s.create_failed do |object, options|
82
+ render :xml => object.errors, :status => :unprocessable_entity
83
+ end
84
+
85
+ s.update do |object, options|
86
+ head :ok
87
+ end
88
+
89
+ s.update_failed do |object, options|
90
+ render :xml => object.errors, :status => :unprocessable_entity
91
+ end
92
+
93
+ s.destroy do |object, options|
94
+ head :ok
95
+ end
96
+
97
+ s.destroy_failed do |object, options|
98
+ render :xml => object.errors, :status => :unprocessable_entity
99
+ end
100
+
101
+ end
102
+
103
+ ::RenderAny.register :json do |s|
104
+
105
+ s.default do |object, options|
106
+ render :json => object.to_json(options)
107
+ end
108
+
109
+ s.not_found do |object, options|
110
+ render_optional_error_file :not_found
111
+ end
112
+
113
+ s.unprocessable_entity do |object, options|
114
+ render_optional_error_file :unprocessable_entity
115
+ end
116
+
117
+ s.create do |object, options|
118
+ render :json => object.to_json(options), :status => :created, :location => object
119
+ end
120
+
121
+ s.create_failed do |object, options|
122
+ render :json => object.errors, :status => :unprocessable_entity
123
+ end
124
+
125
+ s.update do |object, options|
126
+ head :ok
127
+ end
128
+
129
+ s.update_failed do |object, options|
130
+ render :json => object.errors, :status => :unprocessable_entity
131
+ end
132
+
133
+ s.destroy do |object, options|
134
+ head :ok
135
+ end
136
+
137
+ s.destroy_failed do |object, options|
138
+ render :json => object.errors, :status => :unprocessable_entity
139
+ end
140
+
141
+ end
142
+
143
+ ::RenderAny.register :yaml do |s|
144
+
145
+ s.default do |object, options|
146
+ render :text => object.to_yaml(options), :content_type => 'text/yaml'
147
+ end
148
+
149
+ s.not_found do |object, options|
150
+ render_optional_error_file :not_found
151
+ end
152
+
153
+ s.unprocessable_entity do |object, options|
154
+ render_optional_error_file :unprocessable_entity
155
+ end
156
+
157
+ s.create do |object, options|
158
+ render :text => object.to_yaml(options), :content_type => 'text/yaml', :status => :created, :location => object
159
+ end
160
+
161
+ s.create_failed do |object, options|
162
+ render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
163
+ end
164
+
165
+ s.update do |object, options|
166
+ head :ok
167
+ end
168
+
169
+ s.update_failed do |object, options|
170
+ render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
171
+ end
172
+
173
+ s.destroy do |object, options|
174
+ head :ok
175
+ end
176
+
177
+ s.destroy_failed do |object, options|
178
+ render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
179
+ end
180
+
181
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :render_any do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class RenderAnyTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simonmenke-render_any
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: simon.menke@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - test/render_any_test.rb
26
+ - test/test_helper.rb
27
+ - lib/render_any/action_controller/render_any.rb
28
+ - lib/render_any/action_controller/render_any_failable.rb
29
+ - lib/render_any/serializer.rb
30
+ - lib/render_any.rb
31
+ - rails/init.rb
32
+ - tasks/render_any_tasks.rake
33
+ - MIT-LICENSE.txt
34
+ - README.rdoc
35
+ has_rdoc: true
36
+ homepage: http://github.com/simonmenke/render_any
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: render_any
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Automaticaly render an appropriate response.
61
+ test_files:
62
+ - test/render_any_test.rb
63
+ - test/test_helper.rb