![]() |
# Module: Capcode::Helpers [ "README", "AUTHORS", "COPYING", "lib/capcode.rb", nil].each do Capcode.view_html Capcode::Views.view_html Capcode::Helpers.view_html Capcode::HTTPError.view_html Capcode::RenderError.view_html Capcode::RouteError.view_html Capcode::ParameterError.view_html end |
Helpers contains methods available in your controllers

Builds an URL route to a controller or a path
if you declare the controller Hello :
module Capcode
class Hello < Route '/hello/(.*)'
...
end
end
then
URL( Hello, "you" ) # => /hello/you
[ show source ]
# File lib/capcode.rb, line 126
126: def URL( klass, *a )
127: path = nil
128: a = a.delete_if{ |x| x.nil? }
129:
130: if klass.class == Class
131: Capcode.routes.each do |p, k|
132: path = p if k.class == klass
133: end
134: else
135: path = klass
136: end
137:
138: path+((a.size>0)?("/"+a.join("/")):(""))
139: end

Calling content_for stores a block of markup in an identifier.
[ show source ]
# File lib/capcode.rb, line 142
142: def content_for( x )
143: if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
144: yield
145: end
146: end

Help you to return a JSON response
module Capcode
class JsonResponse < Route '/json/([^\/]*)/(.*)'
def get( arg1, arg2 )
json( { :1 => arg1, :2 => arg2 })
end
end
end
[ show source ]
# File lib/capcode.rb, line 90
90: def json( d ) ## DELETE THIS IN 1.0.0
91: warn( "json is deprecated, please use `render( :json => ... )'" )
92: @response['Content-Type'] = 'application/json'
93: d.to_json
94: end

Send a redirect response
module Capcode
class Hello < Route '/hello/(.*)'
def get( you )
if you.nil?
redirect( WhoAreYou )
else
...
end
end
end
end
[ show source ]
# File lib/capcode.rb, line 109
109: def redirect( klass, *a )
110: [302, {'Location' => URL(klass, *a)}, '']
111: end

Render a view
render‘s parameter can be a Hash or a string. Passing a string is equivalent to do
render( :text => string )
If you want to use a specific renderer, use one of this options :
If you want to use a specific layout, you can specify it with option
:layout
[ show source ]
# File lib/capcode.rb, line 52
52: def render( h )
53: if h.class == Hash
54: render_type = nil
55:
56: h.keys.each do |k|
57: if self.respond_to?("render_#{k.to_s}")
58: unless render_type.nil?
59: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{k}') !", caller
60: end
61: render_type = k
62: end
63: end
64:
65: if render_type.nil?
66: raise Capcode::RenderError, "Renderer type not specified!", caller
67: end
68:
69: render_name = h.delete(render_type)
70:
71: begin
72: self.send( "render_#{render_type.to_s}", render_name, h )
73: rescue => e
74: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
75: end
76: else
77: render( :text => h )
78: end
79: end