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 47
47:     def self.args
48:       @args ||= nil
49:     end

[Source]

    # File lib/capcode.rb, line 50
50:     def self.args=(x)
51:       @args = x
52:     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 185
185:     def URL( klass, *a )
186:       path = nil
187:       a = a.delete_if{ |x| x.nil? }
188:       
189:       if klass.class == Class
190:         Capcode.routes.each do |p, k|
191:           path = p if k.class == klass
192:         end
193:       else
194:         path = klass
195:       end
196:       
197:       (ENV['RACK_BASE_URI']||'')+path+((a.size>0)?("/"+a.join("/")):(""))
198:     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 232
232:     def content_for( x )
233:       #if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
234:       if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
235:         yield
236:       end
237:     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 128
128:     def json( d ) ## DELETE THIS IN 1.0.0
129:       warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" )
130:       @response['Content-Type'] = 'application/json'
131:       d.to_json
132:     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 161
161:     def redirect( klass, *a )
162:       httpCode = 302
163: 
164:       if( klass.class == Fixnum )
165:         httpCode = klass
166:         klass = a.shift
167:       end
168: 
169:       [httpCode, {'Location' => URL(klass, *a)}, '']
170:     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 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 80
 80:     def render( hash )
 81:       if hash.class == Hash
 82:         render_type = nil
 83:         
 84:         hash.keys.each do |key|
 85:           if self.respond_to?("render_#{key.to_s}")
 86:             unless render_type.nil?
 87:               raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
 88:             end
 89:             render_type = key
 90:           end
 91:         end
 92:         
 93:         if render_type.nil?
 94:           raise Capcode::RenderError, "Renderer type not specified!", caller
 95:         end
 96: 
 97:         unless self.respond_to?("render_#{render_type.to_s}")
 98:           raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
 99:         end
100: 
101:         render_name = hash.delete(render_type)
102:         content_type = hash.delete(:content_type)
103:         unless content_type.nil?
104:           @response['Content-Type'] = content_type
105:         end
106: 
107:         begin
108:           self.send( "render_#{render_type.to_s}", render_name, hash )
109:         rescue => e
110:           raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
111:         end
112:       else
113:         render( :text => hash )
114:       end
115:     end

Return information about the static directory

[Source]

     # File lib/capcode.rb, line 243
243:     def static
244:       { 
245:         :uri => Capcode.static,
246:         :path => File.expand_path( File.join(".", Capcode.static ) )
247:       }
248:     end

[Validate]