Module | Capcode::Helpers |
In: |
lib/capcode.rb
lib/capcode/render/text.rb lib/capcode/helpers/auth.rb |
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( Capcode::Hello, "you" ) # => /hello/you
# File lib/capcode.rb, line 195 195: def URL( klass, *a ) 196: path = nil 197: a = a.delete_if{ |x| x.nil? } 198: 199: if klass.class == Class 200: Capcode.routes.each do |p, k| 201: path = p if k.class == klass 202: end 203: else 204: path = klass 205: end 206: 207: (ENV['RACK_BASE_URI']||'')+path+((a.size>0)?("/"+a.join("/")):("")) 208: end
Calling content_for stores a block of markup in an identifier.
module Capcode class ContentFor < Route '/' def get render( :markaby => :page, :layout => :layout ) end end end module Capcode::Views def layout html do head do yield :header end body do yield :content end end end def page content_for :header do title "This is the title!" end content_for :content do p "this is the content!" end end end
# File lib/capcode.rb, line 242 242: def content_for( x ) 243: #if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s) 244: if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s) 245: yield 246: end 247: 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
DEPRECATED, please use render( :json => o )
# File lib/capcode.rb, line 138 138: def json( d ) ## DELETE THIS IN 1.0.0 139: warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" ) 140: @response['Content-Type'] = 'application/json' 141: d.to_json 142: end
Send a redirect response
module Capcode class Hello < Route '/hello/(.*)' def get( you ) if you.nil? redirect( WhoAreYou ) else ... end end end end
The first parameter can be a controller class name
redirect( MyController )
it can be a string path
redirect( "/path/to/my/resource" )
it can be an http status code (by default redirect use the http status code 302)
redirect( 304, MyController )
For more informations about HTTP status, see en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
# File lib/capcode.rb, line 171 171: def redirect( klass, *a ) 172: httpCode = 302 173: 174: if( klass.class == Fixnum ) 175: httpCode = klass 176: klass = a.shift 177: end 178: 179: [httpCode, {'Location' => URL(klass, *a)}, ''] 180: 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
If you want to change the Content-Type, you can specify it with option
:content_type
Note that this will not work with the JSON renderer
If you use the WebDav renderer, you can use the option
:resource_class (see http://github.com/georgi/rack_dav for more informations)
# File lib/capcode.rb, line 80 80: def render( hash ) 81: if hash.class == Hash 82: render_type = nil 83: 84: if render_type.nil? 85: hash.keys.each do |key| 86: begin 87: gem "capcode-render-#{key.to_s}" 88: require "capcode/render/#{key.to_s}" 89: rescue Gem::LoadError 90: nil 91: rescue LoadError 92: raise Capcode::RenderError, "Hum... The #{key} renderer is malformated! Please try to install a new version or use an other renderer!", caller 93: end 94: 95: if self.respond_to?("render_#{key.to_s}") 96: unless render_type.nil? 97: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller 98: end 99: render_type = key 100: end 101: end 102: 103: if render_type.nil? 104: raise Capcode::RenderError, "Renderer type not specified!", caller 105: end 106: end 107: unless self.respond_to?("render_#{render_type.to_s}") 108: raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller 109: end 110: 111: render_name = hash.delete(render_type) 112: content_type = hash.delete(:content_type) 113: unless content_type.nil? 114: @response['Content-Type'] = content_type 115: end 116: 117: begin 118: self.send( "render_#{render_type.to_s}", render_name, hash ) 119: rescue => e 120: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller 121: end 122: else 123: render( :text => hash ) 124: end 125: end
Return information about the static directory
# File lib/capcode.rb, line 253 253: def static 254: { 255: :uri => Capcode.static, 256: :path => File.expand_path( File.join(".", Capcode.static ) ) 257: } 258: end