Module Capcode::Helpers
In: lib/capcode.rb
lib/capcode/render/text.rb
lib/capcode/helpers/auth.rb

Helpers contains methods available in your controllers

Methods

URL   args   args=   content_for   json   redirect   render   static  

Included Modules

Authorization

Classes and Modules

Module Capcode::Helpers::Authorization

Public Class methods

@@ARGS = nil

[Source]

    # File lib/capcode.rb, line 46
46:     def self.args
47:       @args ||= nil
48:     end

[Source]

    # File lib/capcode.rb, line 49
49:     def self.args=(x)
50:       @args = x
51:     end

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 213
213:     def URL( klass, *a )
214:       path = nil
215:       a = a.delete_if{ |x| x.nil? }
216:       
217:       if klass.class == Class
218:         Capcode.routes.each do |p, k|
219:           path = p if k.class == klass
220:         end
221:       else
222:         path = klass
223:       end
224:       
225:       (ENV['RACK_BASE_URI']||'')+path+((a.size>0)?("/"+a.join("/")):(""))
226:     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 260
260:     def content_for( x )
261:       #if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
262:       if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
263:         yield
264:       end
265:     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 157
157:     def json( d ) ## DELETE THIS IN 1.0.0
158:       warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" )
159:       render :json => d
160:     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 189
189:     def redirect( klass, *a )
190:       httpCode = 302
191: 
192:       if( klass.class == Fixnum )
193:         httpCode = klass
194:         klass = a.shift
195:       end
196: 
197:       [httpCode, {'Location' => URL(klass, *a)}, '']
198:     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

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)

[Source]

     # File lib/capcode.rb, line 83
 83:     def render( hash )
 84:       if hash.class == Hash
 85:         render_type = nil
 86:         possible_code_renderer = nil
 87:         
 88:         if render_type.nil?
 89:           hash.keys.each do |key|
 90:             begin
 91:               gem "capcode-render-#{key.to_s}"
 92:               require "capcode/render/#{key.to_s}"
 93:             rescue Gem::LoadError
 94:               nil
 95:             rescue LoadError
 96:               raise Capcode::RenderError, "Hum... The #{key} renderer is malformated! Please try to install a new version or use an other renderer!", caller
 97:             end
 98:        
 99:             if self.respond_to?("render_#{key.to_s}")
100:               unless render_type.nil?
101:                 raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
102:               end
103:               render_type = key
104:             end
105:             
106:             if key.class == Fixnum
107:               possible_code_renderer = key
108:             end
109:           end
110:           
111:           if render_type.nil? and possible_code_renderer.nil?
112:             raise Capcode::RenderError, "Renderer type not specified!", caller
113:           end
114:         end
115:         unless self.respond_to?("render_#{render_type.to_s}")
116:           if possible_code_renderer.nil?
117:             raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
118:           else
119:             code = possible_code_renderer
120:             body = hash.delete(possible_code_renderer)
121:             header = {}
122:             hash.each do |k, v|
123:               k = k.to_s.split(/_/).map{|e| e.capitalize}.join("-")
124:               header[k] = v
125:             end
126:             [code, hash, body]
127:           end
128:         else
129:           render_name = hash.delete(render_type)
130:           content_type = hash.delete(:content_type)
131:           unless content_type.nil?
132:             @response['Content-Type'] = content_type
133:           end
134:           
135:           begin
136:             self.send( "render_#{render_type.to_s}", render_name, hash )
137:           rescue => e
138:             raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
139:           end
140:         end
141:       else
142:         render( :text => hash )
143:       end
144:     end

Return information about the static directory

[Source]

     # File lib/capcode.rb, line 271
271:     def static
272:       { 
273:         :uri => Capcode.static,
274:         :path => File.expand_path( File.join(Capcode::Configuration.get(:root), Capcode::Configuration.get(:static) ) )
275:       }
276:     end

[Validate]