| 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 204
204: def URL( klass, *a )
205: path = nil
206: result = {}
207:
208: a = a.delete_if{ |x| x.nil? }
209:
210: if klass.class == Class
211: last_size = 0
212:
213: klass.__urls__[0].each do |cpath, regexp|
214: data = a.clone
215:
216: n = Regexp.new( regexp ).number_of_captures
217: equart = (a.size - n).abs
218:
219: rtable = regexp.dup.gsub( /\\\(/, "" ).gsub( /\\\)/, "" ).split( /\([^\)]*\)/ )
220:
221: rtable.each do |r|
222: if r == ""
223: cpath = cpath + "/#{data.shift}"
224: else
225: cpath = cpath + "/#{r}"
226: end
227: end
228:
229: cpath = (cpath + "/" + data.join( "/" )).gsub( /\/\//, "/" ).gsub( /\/$/, "" )
230: result[equart] = cpath
231: end
232:
233: path = result[result.keys.min]
234: else
235: path = klass
236: end
237:
238: (ENV['RACK_BASE_URI']||'')+path
239: 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 273
273: def content_for( x )
274: if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
275: yield
276: end
277: 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 148
148: def json( d ) ## DELETE THIS IN 1.0.0
149: warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" )
150: render :json => d
151: 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 180
180: def redirect( klass, *a )
181: httpCode = 302
182:
183: if( klass.class == Fixnum )
184: httpCode = klass
185: klass = a.shift
186: end
187:
188: [httpCode, {'Location' => URL(klass, *a)}, '']
189: 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 :
Or you can use a "HTTP code" renderer :
render 200 => "Ok", :server => "Capcode #{Capcode::CAPCOD_VERION}", ...
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 74
74: def render( hash )
75: if hash.class == Hash
76: render_type = nil
77: possible_code_renderer = nil
78:
79: hash.keys.each do |key|
80: begin
81: gem "capcode-render-#{key.to_s}"
82: require "capcode/render/#{key.to_s}"
83: rescue Gem::LoadError
84: nil
85: rescue LoadError
86: raise Capcode::RenderError, "Hum... The #{key} renderer is malformated! Please try to install a new version or use an other renderer!", caller
87: end
88:
89: if self.respond_to?("render_#{key.to_s}")
90: unless render_type.nil?
91: raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
92: end
93: render_type = key
94: end
95:
96: if key.class == Fixnum
97: possible_code_renderer = key
98: end
99: end
100:
101: if render_type.nil? and possible_code_renderer.nil?
102: raise Capcode::RenderError, "Renderer type not specified!", caller
103: end
104:
105: unless self.respond_to?("render_#{render_type.to_s}")
106: if possible_code_renderer.nil?
107: raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
108: else
109: code = possible_code_renderer
110: body = hash.delete(possible_code_renderer)
111: header = {}
112: hash.each do |k, v|
113: k = k.to_s.split(/_/).map{|e| e.capitalize}.join("-")
114: header[k] = v
115: end
116:
117: [code, header, body]
118: end
119: else
120: render_name = hash.delete(render_type)
121: content_type = hash.delete(:content_type)
122: unless content_type.nil?
123: @response['Content-Type'] = content_type
124: end
125:
126: begin
127: self.send( "render_#{render_type.to_s}", render_name, hash )
128: rescue => e
129: raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
130: end
131: end
132: else
133: render( :text => hash )
134: end
135: end
Return information about the static directory
# File lib/capcode.rb, line 283
283: def static
284: {
285: :uri => Capcode.static,
286: :path => File.expand_path( File.join(Capcode::Configuration.get(:root), Capcode::Configuration.get(:static) ) )
287: }
288: end