![]() |
# 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

[ show source ]
# File lib/capcode.rb, line 26
26: def self.root=(p)
27: @@__ROOT_DIRECTORY=p
28: end

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 91
91: def URL( klass, *a )
92: path = nil
93: a = a.delete_if{ |x| x.nil? }
94:
95: if klass.class == Class
96: Capcode.routes.each do |p, k|
97: path = p if k.class == klass
98: end
99: else
100: path = klass
101: end
102:
103: path+((a.size>0)?("/"+a.join("/")):(""))
104: 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 55
55: def json( d ) ## DELETE THIS IN 1.0.0
56: warn( "json is deprecated, please use `render( :json => ... )'" )
57: @response['Content-Type'] = 'application/json'
58: d.to_json
59: 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 74
74: def redirect( klass, *a )
75: [302, {'Location' => URL(klass, *a)}, '']
76: end

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