![]() |
# 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 85
85: def URL( klass, *a )
86: path = nil
87: a = a.delete_if{ |x| x.nil? }
88:
89: if klass.class == Class
90: Capcode.routes.each do |p, k|
91: path = p if k.class == klass
92: end
93: else
94: path = klass
95: end
96:
97: path+((a.size>0)?("/"+a.join("/")):(""))
98: 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 49
49: def json( d )
50: warn( "json is deprecated, please use `render( :json => ... )'" )
51: @response['Content-Type'] = 'application/json'
52: d.to_json
53: 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 68
68: def redirect( klass, *a )
69: [302, {'Location' => URL(klass, *a)}, '']
70: end

Render a view
[ show source ]
# File lib/capcode.rb, line 25
25: def render( h )
26: if h.class == Hash
27: t = (h.keys)[0]
28: v = h[t]
29: begin
30: self.send( "render_#{t.to_s}", v )
31: rescue => e
32: raise Capcode::RenderError, "Error rendering `#{t.to_s}' : #{e.message}", caller
33: end
34: else
35: return self.send(h.to_s)
36: end
37: end