![]() |
# 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 35
35: def self.root=(p)
36: @@__ROOT_DIRECTORY=p
37: 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 128
128: def URL( klass, *a )
129: path = nil
130: a = a.delete_if{ |x| x.nil? }
131:
132: if klass.class == Class
133: Capcode.routes.each do |p, k|
134: path = p if k.class == klass
135: end
136: else
137: path = klass
138: end
139:
140: path+((a.size>0)?("/"+a.join("/")):(""))
141: end

Calling content_for stores a block of markup in an identifier.
[ show source ]
# File lib/capcode.rb, line 144
144: def content_for( x )
145: if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
146: yield
147: end
148: 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 92
92: def json( d ) ## DELETE THIS IN 1.0.0
93: warn( "json is deprecated, please use `render( :json => ... )'" )
94: @response['Content-Type'] = 'application/json'
95: d.to_json
96: 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 111
111: def redirect( klass, *a )
112: [302, {'Location' => URL(klass, *a)}, '']
113: 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 54
54: def render( h )
55: if h.class == Hash
56: render_type = nil
57:
58: h.keys.each do |k|
59: if self.respond_to?("render_#{k.to_s}")
60: unless render_type.nil?
61: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{k}') !", caller
62: end
63: render_type = k
64: end
65: end
66:
67: if render_type.nil?
68: raise Capcode::RenderError, "Renderer type not specified!", caller
69: end
70:
71: render_name = h.delete(render_type)
72:
73: begin
74: self.send( "render_#{render_type.to_s}", render_name, h )
75: rescue => e
76: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
77: end
78: else
79: render( :text => h )
80: end
81: end