Module Capcode::Helpers
In: lib/capcode.rb
lib/capcode/render/erb.rb
lib/capcode/render/haml.rb
lib/capcode/render/json.rb
lib/capcode/render/markaby.rb
lib/capcode/render/sass.rb
lib/capcode/render/static.rb
lib/capcode/render/text.rb
lib/capcode/render/webdav.rb
lib/capcode/render/xml.rb
lib/capcode/helpers/auth.rb

Helpers contains methods available in your controllers

Methods

URL   content_for   json   redirect   render   render_webdav   static  

Included Modules

Authorization

Classes and Modules

Module Capcode::Helpers::Authorization

Public Instance methods

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

[Source]

     # File lib/capcode.rb, line 164
164:     def URL( klass, *a )
165:       path = nil
166:       a = a.delete_if{ |x| x.nil? }
167:       
168:       if klass.class == Class
169:         Capcode.routes.each do |p, k|
170:           path = p if k.class == klass
171:         end
172:       else
173:         path = klass
174:       end
175:       
176:       (ENV['RACK_BASE_URI']||'')+path+((a.size>0)?("/"+a.join("/")):(""))
177:     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

[Source]

     # File lib/capcode.rb, line 211
211:     def content_for( x )
212:       if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
213:         yield
214:       end
215:     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 )

[Source]

     # File lib/capcode.rb, line 107
107:     def json( d ) ## DELETE THIS IN 1.0.0
108:       warn( "json is deprecated, please use `render( :json => ... )'" )
109:       @response['Content-Type'] = 'application/json'
110:       d.to_json
111:     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

[Source]

     # File lib/capcode.rb, line 140
140:     def redirect( klass, *a )
141:       httpCode = 302
142: 
143:       if( klass.class == Fixnum )
144:         httpCode = klass
145:         klass = a.shift
146:       end
147: 
148:       [httpCode, {'Location' => URL(klass, *a)}, '']
149:     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 :

  • :markaby => :my_func : :my_func must be defined in Capcode::Views
  • :erb => :my_erb_file : this suppose that‘s my_erb_file.rhtml exist in erb_path
  • :haml => :my_haml_file : this suppose that‘s my_haml_file.haml exist in haml_path
  • :sass => :my_sass_file : this suppose that‘s my_sass_file.sass exist in sass_path
  • :text => "my text"
  • :json => MyObject : this suppose that‘s MyObject respond to .to_json
  • :static => "my_file.xxx" : this suppose that‘s my_file.xxx exist in the static directory
  • :xml => :my_func : :my_func must be defined in Capcode::Views
  • :webdav => /path/to/root

If you want to use a specific layout, you can specify it with option

  :layout

If you use the WebDav renderer, you can use the option

  :resource_class (see http://github.com/georgi/rack_dav for more informations)

[Source]

    # File lib/capcode.rb, line 63
63:     def render( hash )
64:       if hash.class == Hash
65:         render_type = nil
66:         
67:         hash.keys.each do |key|
68:           if self.respond_to?("render_#{key.to_s}")
69:             unless render_type.nil?
70:               raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
71:             end
72:             render_type = key
73:           end
74:         end
75:         
76:         if render_type.nil?
77:           raise Capcode::RenderError, "Renderer type not specified!", caller
78:         end
79: 
80:         unless self.respond_to?("render_#{render_type.to_s}")
81:           raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
82:         end
83: 
84:         render_name = hash.delete(render_type)
85: 
86:         begin
87:           self.send( "render_#{render_type.to_s}", render_name, hash )
88:         rescue => e
89:           raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
90:         end
91:       else
92:         render( :text => hash )
93:       end
94:     end

[Source]

    # 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

[Source]

     # File lib/capcode.rb, line 221
221:     def static
222:       { 
223:         :uri => Capcode.static,
224:         :path => File.expand_path( File.join(".", Capcode.static ) )
225:       }
226:     end

[Validate]