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