![]() |
# 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 27
27: def self.root=(p)
28: @@__ROOT_DIRECTORY=p
29: 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 120
120: def URL( klass, *a )
121: path = nil
122: a = a.delete_if{ |x| x.nil? }
123:
124: if klass.class == Class
125: Capcode.routes.each do |p, k|
126: path = p if k.class == klass
127: end
128: else
129: path = klass
130: end
131:
132: path+((a.size>0)?("/"+a.join("/")):(""))
133: end

Calling content_for stores a block of markup in an identifier.
[ show source ]
# File lib/capcode.rb, line 136
136: def content_for( x )
137: if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
138: yield
139: end
140: 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 84
84: def json( d ) ## DELETE THIS IN 1.0.0
85: warn( "json is deprecated, please use `render( :json => ... )'" )
86: @response['Content-Type'] = 'application/json'
87: d.to_json
88: 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 103
103: def redirect( klass, *a )
104: [302, {'Location' => URL(klass, *a)}, '']
105: 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 46
46: def render( h )
47: if h.class == Hash
48: render_type = nil
49:
50: h.keys.each do |k|
51: if self.respond_to?("render_#{k.to_s}")
52: unless render_type.nil?
53: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{k}') !", caller
54: end
55: render_type = k
56: end
57: end
58:
59: if render_type.nil?
60: raise Capcode::RenderError, "Renderer type not specified!", caller
61: end
62:
63: render_name = h.delete(render_type)
64:
65: begin
66: self.send( "render_#{render_type.to_s}", render_name, h )
67: rescue => e
68: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
69: end
70: else
71: render( :text => h )
72: end
73: end