class RuGUI::BaseController

Base class for all controllers.

Attributes

controllers[RW]
main_models[RW]
models[RW]
parent_controller[RW]
views[RW]

Public Class Methods

controllers(*names) click to toggle source
# File lib/rugui/base_controller.rb, line 124
def controllers(*names)
  register(:controller, *names)
end
main_models(*names) click to toggle source
# File lib/rugui/base_controller.rb, line 116
def main_models(*names)
  register(:main_model, *names)
end
models(*names) click to toggle source
# File lib/rugui/base_controller.rb, line 112
def models(*names)
  register(:model, *names)
end
new(parent_controller = nil) click to toggle source
# 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
views(*names) click to toggle source
# File lib/rugui/base_controller.rb, line 120
def views(*names)
  register(:view, *names)
end

Public Instance Methods

framework_adapter() click to toggle source

Returns the framework_adapter for this class.

# File lib/rugui/base_controller.rb, line 47
def framework_adapter
  framework_adapter_for('BaseController')
end
main_controller() click to toggle source

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
post_registration() click to toggle source

Called after the controller is registered in another one.

# File lib/rugui/base_controller.rb, line 96
def post_registration
end
register_controller(controller, name = nil) click to toggle source

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
register_main_model(model_name, name = nil) click to toggle source

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
register_model(model, name = nil) click to toggle source

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
register_view(view, name = nil) click to toggle source

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

setup_controllers() click to toggle source

Subclasses should reimplement this to register or initialize controllers.

# File lib/rugui/base_controller.rb, line 151
def setup_controllers
end
setup_main_models() click to toggle source

Subclasses should reimplement this to register or initialize main models.

# File lib/rugui/base_controller.rb, line 139
def setup_main_models
end
setup_models() click to toggle source

Subclasses should reimplement this to register or initialize models.

# File lib/rugui/base_controller.rb, line 133
def setup_models
end
setup_views() click to toggle source

Subclasses should reimplement this to register or initialize views.

# File lib/rugui/base_controller.rb, line 145
def setup_views
end

Private Instance Methods

after_register_controller(controller, name) click to toggle source
# File lib/rugui/base_controller.rb, line 169
def after_register_controller(controller, name)
  controller.parent_controller = self
  controller.post_registration
end
after_register_main_model(model, name) click to toggle source
# File lib/rugui/base_controller.rb, line 160
def after_register_main_model(model, name)
  after_register_model(model, name)
end
after_register_model(model, name) click to toggle source
# File lib/rugui/base_controller.rb, line 155
def after_register_model(model, name)
  model.register_observer(self, name)
  model.post_registration(self)
end
after_register_view(view, name) click to toggle source
# File lib/rugui/base_controller.rb, line 164
def after_register_view(view, name)
  view.register_controller(self)
  view.post_registration(self)
end
controller_name() click to toggle source
# 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
create_instance_arguments_for_controller() click to toggle source
# File lib/rugui/base_controller.rb, line 174
def create_instance_arguments_for_controller
  [self]
end
default_view_name() click to toggle source
# File lib/rugui/base_controller.rb, line 189
def default_view_name
  "#{controller_name}_view"
end
find_main_controller() click to toggle source

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
get_instance_for_main_model(name) click to toggle source
# 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
register_default_view() click to toggle source
# 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
should_register_default_view?() click to toggle source
# File lib/rugui/base_controller.rb, line 198
def should_register_default_view?
  RuGUI.configuration.automatically_register_conventionally_named_views
end