class RuGUI::BaseController
Base class for all controllers.
Attributes
Public Class Methods
# File lib/rugui/base_controller.rb, line 124 def controllers(*names) register(:controller, *names) end
# File lib/rugui/base_controller.rb, line 116 def main_models(*names) register(:main_model, *names) end
# File lib/rugui/base_controller.rb, line 112 def models(*names) register(:model, *names) end
# File lib/rugui/base_controller.rb, line 17 def initialize(parent_controller = nil) @models = {} @main_models = {} @views = {} @controllers = {} if parent_controller.nil? @parent_controller = self else @parent_controller = parent_controller end register_all :model setup_models register_default_view if should_register_default_view? register_all :view setup_views register_all :controller setup_controllers register_all :main_model setup_main_models end
# File lib/rugui/base_controller.rb, line 120 def views(*names) register(:view, *names) end
Public Instance Methods
Returns the framework_adapter
for this class.
# File lib/rugui/base_controller.rb, line 47 def framework_adapter framework_adapter_for('BaseController') end
Returns the main controller instance.
This is an useful way to quickly access the main controller from any other controller. Since applications may have only one main controller and it is always the ‘root’ of the tree of controllers, this provides indirect access to any other controller in the application.
NOTE: The main controller is cached, so that subsequent calls are faster.
# File lib/rugui/base_controller.rb, line 107 def main_controller @main_controller ||= find_main_controller end
Called after the controller is registered in another one.
# File lib/rugui/base_controller.rb, line 96 def post_registration end
Registers a child controller for this controller.
If the given controller is a string or symbol, it will be camelized and a new instance of the controller class will be created.
# File lib/rugui/base_controller.rb, line 89 def register_controller(controller, name = nil) register(:controller, controller, name) end
Registers a main model for this controller.
Only model names (as string or symbol) should be passed. Optionally a different name may be given. If the main controller doesn’t have a model registered or if this is the main controller a NoMethodError exception will be raised.
# File lib/rugui/base_controller.rb, line 69 def register_main_model(model_name, name = nil) register(:main_model, model_name, name) end
Registers a model for this controller.
If the given model is a string or symbol, it will be camelized and a new instance of the model class will be created.
# File lib/rugui/base_controller.rb, line 57 def register_model(model, name = nil) register(:model, model, name) end
Registers a view for this controller.
If the given view is a string or symbol, it will be camelized and a new instance of the view class will be created.
# File lib/rugui/base_controller.rb, line 79 def register_view(view, name = nil) register(:view, view, name) end
Protected Instance Methods
Subclasses should reimplement this to register or initialize controllers.
# File lib/rugui/base_controller.rb, line 151 def setup_controllers end
Subclasses should reimplement this to register or initialize main models.
# File lib/rugui/base_controller.rb, line 139 def setup_main_models end
Subclasses should reimplement this to register or initialize models.
# File lib/rugui/base_controller.rb, line 133 def setup_models end
Subclasses should reimplement this to register or initialize views.
# File lib/rugui/base_controller.rb, line 145 def setup_views end
Private Instance Methods
# File lib/rugui/base_controller.rb, line 169 def after_register_controller(controller, name) controller.parent_controller = self controller.post_registration end
# File lib/rugui/base_controller.rb, line 160 def after_register_main_model(model, name) after_register_model(model, name) end
# File lib/rugui/base_controller.rb, line 155 def after_register_model(model, name) model.register_observer(self, name) model.post_registration(self) end
# File lib/rugui/base_controller.rb, line 164 def after_register_view(view, name) view.register_controller(self) view.post_registration(self) end
# File lib/rugui/base_controller.rb, line 193 def controller_name match = self.class.name.underscore.match(/([\w_]*)_controller/) match ? match[1] : self.class.name end
# File lib/rugui/base_controller.rb, line 174 def create_instance_arguments_for_controller [self] end
# File lib/rugui/base_controller.rb, line 189 def default_view_name "#{controller_name}_view" end
Navigates through the controllers hierarchy trying to find the main controller (i.e., a class that extends RuGUI::BaseMainController
).
# File lib/rugui/base_controller.rb, line 204 def find_main_controller if self.parent_controller.is_a?(RuGUI::BaseMainController) self.parent_controller elsif self.parent_controller == self return nil else self.parent_controller.main_controller end end
# File lib/rugui/base_controller.rb, line 178 def get_instance_for_main_model(name) main_controller.send(name) # should raise an error if main_controller doesn't have that main model. end
# File lib/rugui/base_controller.rb, line 182 def register_default_view default_view_name.camelize.constantize # Check if we can constantize view name, if this fails a NameError exception is thrown. register_view default_view_name rescue NameError # No default view for this controller, nothing to do. end
# File lib/rugui/base_controller.rb, line 198 def should_register_default_view? RuGUI.configuration.automatically_register_conventionally_named_views end