Class: Mousevc::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/mousevc/router.rb

Overview

Router routes requests to the controller method. Instantiated by the Mousevc::App class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Router

Creates a new Mousevc::Router instance

Parameters:

  • options (Hash) (defaults to: {})

    expects the following keys:

    • :controller => [String] name of default controller class

    • :model => [String] name of default model class

    • :action => [Symbol] method to call on default controller

    • :views => [String] relative path to views directory



47
48
49
50
51
52
# File 'lib/mousevc/router.rb', line 47

def initialize(options={})
	@controller = options[:controller] ? options[:controller] : 'Controller'
	@model = options[:model] ? options[:model] : 'Model'
	@action = options[:action] ? options[:action] : :hello_mousevc
	@views = options[:views]
end

Instance Attribute Details

#actionSymbol

Note:

Set this variable to the symbol name of the method to call on the next controller

Returns the action sent to the controller method.

Returns:

  • (Symbol)

    the action sent to the controller method



36
37
38
# File 'lib/mousevc/router.rb', line 36

def action
  @action
end

#controllerString

Note:

Set this variable to the string name of the controller class to be instantiated upon the next application loop

Returns the instance of the current controller.

Returns:

  • (String)

    the instance of the current controller



19
20
21
# File 'lib/mousevc/router.rb', line 19

def controller
  @controller
end

#modelString, Symbol

Note:

Set this variable to the string name of the model class to be instantiated upon the next application loop

Note:

Can be used to retrieve a model from the Mousevc::Persistence class when set to a symbol.

Returns the instance of the current model.

Returns:

  • (String, Symbol)

    the instance of the current model.



28
29
30
# File 'lib/mousevc/router.rb', line 28

def model
  @model
end

Instance Method Details

#routeObject

Routes by:

  1. creating an instance of the current controller in the @controller attribute

  2. creating an instance of the current model in the @model attribute

  3. creating a new or finding the desired model

  4. passing that controller that model instance, a view instance, and an instance of self

  5. sending the controller the current action in @action



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mousevc/router.rb', line 63

def route
	model = Persistence.get(@controller.to_sym)
	model = Persistence.get(@model) if @model.is_a?(Symbol)
	unless model
		model = Mousevc.factory(@model).new
		Persistence.set(@controller.to_sym, model)
	end

	view = View.new(:dir => @views)

	controller = Mousevc.factory(@controller).new(
		:view => view,
		:model => model,
		:router => self
	)
	controller.send(@action)
end