Helpers contains methods available in your controllers
Set the path to ERB files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0
# File lib/capcode/render/erb.rb, line 7
7: def self.erb_path=( p )
8: warn "Capcode::Helpers.erb_path is deprecated and will be removed in version 1.0, please use `set :erb'"
9: Capcode.set :erb, p
10: end
Set the path to Haml files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0
# File lib/capcode/render/haml.rb, line 7
7: def self.haml_path=( p )
8: warn "Capcode::Helpers.haml_path is deprecated and will be removed in version 1.0, please use `set :haml'"
9: Capcode.set :haml, p
10: end
Set the path to Sass files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0
# File lib/capcode/render/sass.rb, line 7
7: def self.sass_path=( p )
8: warn "Capcode::Helpers.sass_path is deprecated and will be removed in version 1.0, please use `set :sass'"
9: Capcode.set :sass, p
10: 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( Capcode::Hello, "you" ) # => /hello/you
# File lib/capcode.rb, line 181
181: def URL( klass, *a )
182: path = nil
183: a = a.delete_if{ |x| x.nil? }
184:
185: if klass.class == Class
186: Capcode.routes.each do |p, k|
187: path = p if k.class == klass
188: end
189: else
190: path = klass
191: end
192:
193: (ENV['RACK_BASE_URI']||'')+path+((a.size>0)?("/"+a.join("/")):(""))
194: 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 228
228: def content_for( x )
229: #if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
230: if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
231: yield
232: end
233: 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 124
124: def json( d ) ## DELETE THIS IN 1.0.0
125: warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" )
126: @response['Content-Type'] = 'application/json'
127: d.to_json
128: 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 157
157: def redirect( klass, *a )
158: httpCode = 302
159:
160: if( klass.class == Fixnum )
161: httpCode = klass
162: klass = a.shift
163: end
164:
165: [httpCode, {'Location' => URL(klass, *a)}, '']
166: 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 76
76: def render( hash )
77: if hash.class == Hash
78: render_type = nil
79:
80: hash.keys.each do |key|
81: if self.respond_to?("render_#{key.to_s}")
82: unless render_type.nil?
83: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
84: end
85: render_type = key
86: end
87: end
88:
89: if render_type.nil?
90: raise Capcode::RenderError, "Renderer type not specified!", caller
91: end
92:
93: unless self.respond_to?("render_#{render_type.to_s}")
94: raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
95: end
96:
97: render_name = hash.delete(render_type)
98: content_type = hash.delete(:content_type)
99: unless content_type.nil?
100: @response['Content-Type'] = content_type
101: end
102:
103: begin
104: self.send( "render_#{render_type.to_s}", render_name, hash )
105: rescue => e
106: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
107: end
108: else
109: render( :text => hash )
110: end
111: end
# File lib/capcode/render/webdav.rb, line 19
19: def render_webdav( f, opts )
20: options = {
21: :resource_class => RackDAV::FileResource,
22: :root => f
23: }.merge(opts)
24:
25: request = Rack::Request.new(env)
26: response = Rack::Response.new
27:
28: begin
29: controller = RackDAV::Controller.new(request, response, options.dup)
30: controller.send(request.request_method.downcase)
31: rescue RackDAV::HTTPStatus::Status => status
32: response.status = status.code
33: end
34:
35: # Strings in Ruby 1.9 are no longer enumerable. Rack still expects the response.body to be
36: # enumerable, however.
37: response.body = [response.body] if not response.body.respond_to? :each
38:
39: response.status = response.status ? response.status.to_i : 200
40: response.finish
41:
42: [response.status, response.header, response.body]
43: end
Return information about the static directory
# File lib/capcode.rb, line 239
239: def static
240: {
241: :uri => Capcode.static,
242: :path => File.expand_path( File.join(".", Capcode.static ) )
243: }
244: end