Class: Ramaze::Controller

Inherits:
Object show all
Includes:
Innate::Node, Innate::Traited
Defined in:
lib/ramaze/controller.rb

Overview

Ramaze::Controller is the base controller of all controllers when developing applications in Ramaze. It acts as a nice wrapper around Innate::Node and allows for a more traditional MVC approach.

Example

 class Posts < Ramaze::Controller
   map '/posts'

   def index

   end
 end

Author:

Since:

Direct Known Subclasses

Controller, DefaultController

Constant Summary

CONTROLLER_LIST =

Since:

  • 04-01-2009

Set.new
IRREGULAR_MAPPING =

Hash containing the names of two common controller names and the URIs they should be mapped to.

Author:

  • Michael Fellinger

Since:

  • 04-01-2009

{
  'Controller'     => nil,
  'MainController' => '/'
}

Class Method Summary (collapse)

Class Method Details

+ (Ramaze::App) app

Returns the application to which the controller belongs to.

Returns:

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



186
187
188
# File 'lib/ramaze/controller.rb', line 186

def self.app
  App[ancestral_trait[:app]]
end

+ (Object) engine(name)

Sets the view engine to use for pages with a content type of text/html.

Examples:

class Posts < Ramaze::Controller
  engine :etanni
end

Parameters:

  • name (#to_sym)

    The name of the view engine to use.

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



122
123
124
# File 'lib/ramaze/controller.rb', line 122

def self.engine(name)
  provide(:html, name.to_sym)
end

+ (String) generate_mapping(klass_name = self.name)

Generates a URI for the full namespace of a class. If a class is named A::B::C the URI would be /a/b/c.

Parameters:

  • klass_name (String) (defaults to: self.name)

    The name of the class for which to generate the mapping, defaults to the current class.

Returns:

  • (String)

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



147
148
149
150
151
152
153
154
155
156
# File 'lib/ramaze/controller.rb', line 147

def self.generate_mapping(klass_name = self.name)
  chunks = klass_name.to_s.split(/::/)
  return if chunks.empty?

  last = chunks.last
  return IRREGULAR_MAPPING[last] if IRREGULAR_MAPPING.key?(last)

  last.sub!(/Controller$/, '')
  '/' << chunks.map{|chunk| chunk.snake_case }.join('/')
end

+ (Object) inherited(into)

Modifies the extending class so that it’s properly set up to be used as a controller.

Parameters:

  • into (Class)

    The class that extended Ramaze::Controller (or a sub class).

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



59
60
61
62
63
64
# File 'lib/ramaze/controller.rb', line 59

def self.inherited(into)
  Innate::Node.included(into)
  into.helper(:layout)
  CONTROLLER_LIST << into
  into.trait :skip_node_map => true
end

+ (Object) map(location, app_name = nil)

Maps the current class to the specified location.

Parameters:

  • location (String)

    The URI to map the controller to.

  • app_name (String) (defaults to: nil)

    The name of the application the controller belongs to.

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ramaze/controller.rb', line 167

def self.map(location, app_name = nil)
  if app_name
    trait :app => app_name
  else
    app_name = ancestral_trait[:app]
  end

  trait :skip_controller_map => true

  App.find_or_create(app_name).map(location, self)
end

+ (String) mapping

Returns the URI a controller is mapped to.

Returns:

  • (String)

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



133
134
135
# File 'lib/ramaze/controller.rb', line 133

def self.mapping
  Ramaze.to(self)
end

+ (Innate::Options) options

Returns all the options for the application the controller belongs to.

Returns:

  • (Innate::Options)

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



197
198
199
200
# File 'lib/ramaze/controller.rb', line 197

def self.options
  return unless app = self.app
  app.options
end

+ (Object) setup

Sets all the controllers up and loads a default controller in case no custom ones have been specified.

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ramaze/controller.rb', line 73

def self.setup
  case CONTROLLER_LIST.size
  when 0
    require 'ramaze/controller/default'
  when 1
    controller = CONTROLLER_LIST.to_a.first

    begin
      controller.mapping
    rescue
      controller.map '/'
    end

    controller.setup_procedure
  else
    CONTROLLER_LIST.each do |controller|
      controller.setup_procedure
    end
  end
end

+ (Object) setup_procedure

Method that’s used to setup each controller, called by Ramaze::Controller.setup.

Author:

  • Michael Fellinger

Since:

  • 04-01-2009



101
102
103
104
105
106
107
108
# File 'lib/ramaze/controller.rb', line 101

def self.setup_procedure
  unless ancestral_trait[:provide_set]
    engine(:etanni)
    trait(:provide_set => false)
  end

  map(generate_mapping(name)) unless trait[:skip_controller_map]
end