# Module: Capcode::Helpers
[ "README", "AUTHORS", "COPYING", "lib/capcode.rb", nil].each do
Capcode.view_html
Capcode::Views.view_html
Capcode::Helpers.view_html
Capcode::HTTPError.view_html
Capcode::RenderError.view_html
Capcode::RouteError.view_html
Capcode::ParameterError.view_html
end

Module Capcode::Helpers

(in files lib/capcode.rb )

Helpers contains methods available in your controllers

Includes

Methods

Public Class method: root=(p)

    # File lib/capcode.rb, line 35
35:     def self.root=(p)
36:       @@__ROOT_DIRECTORY=p
37:     end

Public Instance method: URL( klass, *a )

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( Hello, "you" ) # => /hello/you
     # File lib/capcode.rb, line 128
128:     def URL( klass, *a )
129:       path = nil
130:       a = a.delete_if{ |x| x.nil? }
131:       
132:       if klass.class == Class
133:         Capcode.routes.each do |p, k|
134:           path = p if k.class == klass
135:         end
136:       else
137:         path = klass
138:       end
139:       
140:       path+((a.size>0)?("/"+a.join("/")):(""))
141:     end

Public Instance method: content_for( x ) {|| ...}

Calling content_for stores a block of markup in an identifier.

     # File lib/capcode.rb, line 144
144:     def content_for( x )
145:       if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
146:         yield
147:       end
148:     end

Public Instance method: json( d )

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

Public Instance method: redirect( klass, *a )

Send a redirect response

  module Capcode
    class Hello < Route '/hello/(.*)'
      def get( you )
        if you.nil?
          redirect( WhoAreYou )
        else
          ...
        end
      end
    end
  end
     # File lib/capcode.rb, line 111
111:     def redirect( klass, *a )
112:       [302, {'Location' => URL(klass, *a)}, '']
113:     end

Public Instance method: render( h )

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.rhtml exist in haml_path
  • :text => "my text"
  • :json => MyObject : this suppose that‘s MyObject respond to .to_json

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

  :layout
    # File lib/capcode.rb, line 54
54:     def render( h )
55:       if h.class == Hash
56:         render_type = nil
57:         
58:         h.keys.each do |k|
59:           if self.respond_to?("render_#{k.to_s}")
60:             unless render_type.nil?
61:               raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{k}') !", caller
62:             end
63:             render_type = k
64:           end
65:         end
66: 
67:         if render_type.nil?
68:           raise Capcode::RenderError, "Renderer type not specified!", caller
69:         end
70: 
71:         render_name = h.delete(render_type)
72: 
73:         begin
74:           self.send( "render_#{render_type.to_s}", render_name, h )
75:         rescue => e
76:           raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
77:         end
78:       else
79:         render( :text => h )
80:       end
81:     end