render_any 0.0.3
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/lib/render_any.rb +21 -0
- data/lib/render_any/action_controller/render_any.rb +42 -0
- data/lib/render_any/action_controller/render_any_failable.rb +28 -0
- data/lib/render_any/serializer.rb +35 -0
- data/rails/init.rb +197 -0
- data/tasks/render_any_tasks.rake +4 -0
- data/test/render_any_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +65 -0
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
|
@@ -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/rails/init.rb
ADDED
@@ -0,0 +1,197 @@
|
|
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.index do |object, options|
|
30
|
+
render :action => (options[:action] || 'index')
|
31
|
+
end
|
32
|
+
|
33
|
+
s.show do |object, options|
|
34
|
+
render :action => (options[:action] || 'show')
|
35
|
+
end
|
36
|
+
|
37
|
+
s.new do |object, options|
|
38
|
+
render :action => (options[:action] || 'new')
|
39
|
+
end
|
40
|
+
|
41
|
+
s.edit do |object, options|
|
42
|
+
render :action => (options[:action] || 'edit')
|
43
|
+
end
|
44
|
+
|
45
|
+
s.not_found do |object, options|
|
46
|
+
render_optional_error_file :not_found
|
47
|
+
end
|
48
|
+
|
49
|
+
s.unprocessable_entity do |object, options|
|
50
|
+
render_optional_error_file :unprocessable_entity
|
51
|
+
end
|
52
|
+
|
53
|
+
s.create do |object, options|
|
54
|
+
redirect_to object
|
55
|
+
end
|
56
|
+
|
57
|
+
s.create_failed do |object, options|
|
58
|
+
render :action => (options[:action_failed] || 'new')
|
59
|
+
end
|
60
|
+
|
61
|
+
s.update do |object, options|
|
62
|
+
redirect_to object
|
63
|
+
end
|
64
|
+
|
65
|
+
s.update_failed do |object, options|
|
66
|
+
render :action => (options[:action_failed] || 'edit')
|
67
|
+
end
|
68
|
+
|
69
|
+
s.destroy do |object, options|
|
70
|
+
redirect_to [object.class.to_s.underscore.pluralize.to_sym]
|
71
|
+
end
|
72
|
+
|
73
|
+
s.destroy_failed do |object, options|
|
74
|
+
redirect_to object
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
::RenderAny.register :xml do |s|
|
80
|
+
|
81
|
+
s.default do |object, options|
|
82
|
+
render :xml => object.to_xml(options)
|
83
|
+
end
|
84
|
+
|
85
|
+
s.not_found do |object, options|
|
86
|
+
render_optional_error_file :not_found
|
87
|
+
end
|
88
|
+
|
89
|
+
s.unprocessable_entity do |object, options|
|
90
|
+
render_optional_error_file :unprocessable_entity
|
91
|
+
end
|
92
|
+
|
93
|
+
s.create do |object, options|
|
94
|
+
render :xml => object.to_xml(options), :status => :created, :location => object
|
95
|
+
end
|
96
|
+
|
97
|
+
s.create_failed do |object, options|
|
98
|
+
render :xml => object.errors, :status => :unprocessable_entity
|
99
|
+
end
|
100
|
+
|
101
|
+
s.update do |object, options|
|
102
|
+
head :ok
|
103
|
+
end
|
104
|
+
|
105
|
+
s.update_failed do |object, options|
|
106
|
+
render :xml => object.errors, :status => :unprocessable_entity
|
107
|
+
end
|
108
|
+
|
109
|
+
s.destroy do |object, options|
|
110
|
+
head :ok
|
111
|
+
end
|
112
|
+
|
113
|
+
s.destroy_failed do |object, options|
|
114
|
+
render :xml => object.errors, :status => :unprocessable_entity
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
::RenderAny.register :json do |s|
|
120
|
+
|
121
|
+
s.default do |object, options|
|
122
|
+
render :json => object.to_json(options)
|
123
|
+
end
|
124
|
+
|
125
|
+
s.not_found do |object, options|
|
126
|
+
render_optional_error_file :not_found
|
127
|
+
end
|
128
|
+
|
129
|
+
s.unprocessable_entity do |object, options|
|
130
|
+
render_optional_error_file :unprocessable_entity
|
131
|
+
end
|
132
|
+
|
133
|
+
s.create do |object, options|
|
134
|
+
render :json => object.to_json(options), :status => :created, :location => object
|
135
|
+
end
|
136
|
+
|
137
|
+
s.create_failed do |object, options|
|
138
|
+
render :json => object.errors, :status => :unprocessable_entity
|
139
|
+
end
|
140
|
+
|
141
|
+
s.update do |object, options|
|
142
|
+
head :ok
|
143
|
+
end
|
144
|
+
|
145
|
+
s.update_failed do |object, options|
|
146
|
+
render :json => object.errors, :status => :unprocessable_entity
|
147
|
+
end
|
148
|
+
|
149
|
+
s.destroy do |object, options|
|
150
|
+
head :ok
|
151
|
+
end
|
152
|
+
|
153
|
+
s.destroy_failed do |object, options|
|
154
|
+
render :json => object.errors, :status => :unprocessable_entity
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
::RenderAny.register :yaml do |s|
|
160
|
+
|
161
|
+
s.default do |object, options|
|
162
|
+
render :text => object.to_yaml(options), :content_type => 'text/yaml'
|
163
|
+
end
|
164
|
+
|
165
|
+
s.not_found do |object, options|
|
166
|
+
render_optional_error_file :not_found
|
167
|
+
end
|
168
|
+
|
169
|
+
s.unprocessable_entity do |object, options|
|
170
|
+
render_optional_error_file :unprocessable_entity
|
171
|
+
end
|
172
|
+
|
173
|
+
s.create do |object, options|
|
174
|
+
render :text => object.to_yaml(options), :content_type => 'text/yaml', :status => :created, :location => object
|
175
|
+
end
|
176
|
+
|
177
|
+
s.create_failed do |object, options|
|
178
|
+
render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
|
179
|
+
end
|
180
|
+
|
181
|
+
s.update do |object, options|
|
182
|
+
head :ok
|
183
|
+
end
|
184
|
+
|
185
|
+
s.update_failed do |object, options|
|
186
|
+
render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
|
187
|
+
end
|
188
|
+
|
189
|
+
s.destroy do |object, options|
|
190
|
+
head :ok
|
191
|
+
end
|
192
|
+
|
193
|
+
s.destroy_failed do |object, options|
|
194
|
+
render :text => object.errors.to_yaml, :content_type => 'text/yaml', :status => :unprocessable_entity
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: render_any
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Menke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-26 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Automaticaly render an appropriate response.
|
17
|
+
email: simon.menke@gmail.com
|
18
|
+
engine_dependencies: {}
|
19
|
+
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
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
|
+
- test/render_any_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/simonmenke/render_any
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.3
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Automaticaly render an appropriate response.
|
63
|
+
test_files:
|
64
|
+
- test/render_any_test.rb
|
65
|
+
- test/test_helper.rb
|