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

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

Calling content_for stores a block of markup in an identifier.

     # File lib/capcode.rb, line 142
142:     def content_for( x )
143:       if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
144:         yield
145:       end
146:     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 90
90:     def json( d ) ## DELETE THIS IN 1.0.0
91:       warn( "json is deprecated, please use `render( :json => ... )'" )
92:       @response['Content-Type'] = 'application/json'
93:       d.to_json
94:     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 109
109:     def redirect( klass, *a )
110:       [302, {'Location' => URL(klass, *a)}, '']
111:     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 52
52:     def render( h )
53:       if h.class == Hash
54:         render_type = nil
55:         
56:         h.keys.each do |k|
57:           if self.respond_to?("render_#{k.to_s}")
58:             unless render_type.nil?
59:               raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{k}') !", caller
60:             end
61:             render_type = k
62:           end
63:         end
64: 
65:         if render_type.nil?
66:           raise Capcode::RenderError, "Renderer type not specified!", caller
67:         end
68: 
69:         render_name = h.delete(render_type)
70: 
71:         begin
72:           self.send( "render_#{render_type.to_s}", render_name, h )
73:         rescue => e
74:           raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}", caller
75:         end
76:       else
77:         render( :text => h )
78:       end
79:     end

Public Instance method: static()

Return information about the static directory

     # File lib/capcode.rb, line 149
149:     def static
150:       { 
151:         :uri => Capcode.static,
152:         :path => File.expand_path( File.join(".", Capcode.static ) )
153:       }
154:     end