Module Capcode::Helpers
In: lib/capcode.rb
lib/capcode/render/binary.rb
lib/capcode/render/email.rb
lib/capcode/render/erb.rb
lib/capcode/render/haml.rb
lib/capcode/render/json.rb
lib/capcode/render/markaby.rb
lib/capcode/render/mustache.rb
lib/capcode/render/none.rb
lib/capcode/render/redirect.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   args   args=   content_for   erb_path=   haml_path=   json   redirect   render   render_webdav   sass_path=   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

Set the path to ERB files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0

[Source]

    # File lib/capcode/render/erb.rb, line 7
 7:     def self.erb_path=( p )
 8:       warn "Capcode::Helpers.erb_path is deprecated and will be removed in version 1.0, please use `set :erb'"
 9:       Capcode.set :erb, p
10:     end

Set the path to Haml files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0

[Source]

    # File lib/capcode/render/haml.rb, line 11
11:     def self.haml_path=( p )
12:       warn "Capcode::Helpers.haml_path is deprecated and will be removed in version 1.0, please use `set :haml'"
13:       Capcode.set :haml, p
14:     end

Set the path to Sass files. If this path is not set, Capcode will search in the static path. This method is deprecated and will be removed in version 1.0

[Source]

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

[Source]

    # File lib/capcode/render/webdav.rb, line 23
23:     def render_webdav( f, opts )
24:                   options = {
25:                     :resource_class => RackDAV::FileResource,
26:                     :root => f
27:                   }.merge(opts)
28:                               
29:                   request = Rack::Request.new(env)
30:                   response = Rack::Response.new
31:     
32:                   begin
33:                     controller = RackDAV::Controller.new(request, response, options.dup)                  
34:                     controller.send(request.request_method.downcase)
35:                   rescue RackDAV::HTTPStatus::Status => status
36:                     response.status = status.code
37:                   end
38:       
39:                   # Strings in Ruby 1.9 are no longer enumerable.  Rack still expects the response.body to be
40:                   # enumerable, however.
41:                   response.body = [response.body] if not response.body.respond_to? :each
42:       
43:                   response.status = response.status ? response.status.to_i : 200
44:                   response.finish
45:                   
46:                   [response.status, response.header, response.body]
47:                 end

Return information about the static directory

[Source]

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

[Validate]