class Ramaze::Controller
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 An example controller
class Posts < Ramaze::Controller map '/posts' def index end end
@author Michael Fellinger @since 04-01-2009
Constants
- CONTROLLER_LIST
- 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
Public Class Methods
Returns the application to which the controller belongs to.
@author Michael Fellinger @since 04-01-2009 @return [Ramaze::App]
# File lib/ramaze/controller.rb, line 192 def self.app App[ancestral_trait[:app]] end
Sets the view engine to use for pages with a content type of text/html.
@example
class Posts < Ramaze::Controller engine :etanni end
@author Michael Fellinger @since 04-01-2009 @param [#to_sym] name The name of the view engine to use.
# File lib/ramaze/controller.rb, line 121 def self.engine(name) provide(:html, name.to_sym) end
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.
@author Michael Fellinger @since 04-01-2009 @param [String] klass_name The name of the class for which to generate
the mapping, defaults to the current class.
@return [String]
# File lib/ramaze/controller.rb, line 146 def self.generate_mapping(klass_name = self.name) chunks = klass_name.to_s.split(/::/) return if chunks.empty? last = chunks.last if IRREGULAR_MAPPING.key?(last) irregular = IRREGULAR_MAPPING[last] return irregular if irregular.nil? || chunks.size == 1 chunks.pop chunks << irregular end chunks.unshift '' chunks.last.sub!(/Controller$/, '') chunks.map{|chunk| chunk.snake_case }.join('/').squeeze('/') end
Modifies the extending class so that it's properly set up to be used as a controller.
@author Michael Fellinger @since 04-01-2009 @param [Class] into The class that extended Ramaze::Controller (or a sub
class).
# File lib/ramaze/controller.rb, line 58 def self.inherited(into) Innate::Node.included(into) into.helper(:layout) CONTROLLER_LIST << into into.trait :skip_node_map => true end
Maps the current class to the specified location.
@author Michael Fellinger @since 04-01-2009 @param [String] location The URI to map the controller to. @param [String] app_name The name of the application the controller
belongs to.
# File lib/ramaze/controller.rb, line 173 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
Returns the URI a controller is mapped to.
@author Michael Fellinger @since 04-01-2009 @return [String]
# File lib/ramaze/controller.rb, line 132 def self.mapping Ramaze.to(self) end
Returns all the options for the application the controller belongs to.
@author Michael Fellinger @since 04-01-2009 @return [Innate::Options]
# File lib/ramaze/controller.rb, line 203 def self.options return unless app = self.app app.options end
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
# File lib/ramaze/controller.rb, line 72 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 |list_controller| list_controller.setup_procedure end end end
Method that's used to setup each controller, called by ::setup.
@author Michael Fellinger @since 04-01-2009
# File lib/ramaze/controller.rb, line 100 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