Class: N::Dispatcher

Inherits:
Object show all
Defined in:
lib/nitro/dispatcher.rb

Overview

The Dispatcher manages a set of controllers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controllers = nil, apis = nil) ⇒ Dispatcher

Create a new Dispatcher.

Input:

controllers

Either a hash of controller mappings or a single controller that gets mapped to :root.

apis

A hash of apis supported by the Dispatcher.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nitro/dispatcher.rb', line 36

def initialize(controllers = nil, apis = nil)
	@root = 'root'
	
	if controllers and controllers.is_a?(Class) and controllers.ancestors.include?(Controller)
		@controllers = { :root => controllers }
	else
		@controllers = controllers || { :root => SimpleController }
	end

	@apis = apis
end

Instance Attribute Details

#apisObject

APIs map.



23
24
25
# File 'lib/nitro/dispatcher.rb', line 23

def apis
  @apis
end

#controllersObject

The controllers map.



19
20
21
# File 'lib/nitro/dispatcher.rb', line 19

def controllers
  @controllers
end

#rootObject

The root directory.



15
16
17
# File 'lib/nitro/dispatcher.rb', line 15

def root
  @root
end

Instance Method Details

#add_api(api, data) ⇒ Object

Add a new api to the dispatcher

api

API symbol

data

Data for this API [content_type, ..]



75
76
77
# File 'lib/nitro/dispatcher.rb', line 75

def add_api(api, data)
	(@apis ||= {})[api] = data
end

#controller_class_for(key, context) ⇒ Object

Get the controller for the given key. Also handles reloading of controllers.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nitro/dispatcher.rb', line 133

def controller_class_for(key, context)
	klass = @controllers[key]

	if context[:__RELOADED__].nil? and (:full == Rendering.reload) and klass
		def_file = klass::DEF_FILE
		Controller.remove_subclasses
		load(def_file)
		klass = @controllers[key] = Object.const_get(klass.name.intern)
		context[:__RELOADED__] = true
	end

	return klass
end

#dispatch(path, context) ⇒ Object Also known as: split_path

Processes the path and dispatches to the corresponding controller/action pair. The base returned contains a trailing ‘/’.

path

The path to dispatch.

:context

The dispatching context.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nitro/dispatcher.rb', line 89

def dispatch(path, context)
	api = :xhtml

	if @apis
		@apis.each { |k, v| api = k if path.slice!(/#{k}\//) }
	end

	parts = path.split('/')

	case parts.size
		when 0
			# / -> root.index
			base = @root 
			klass = controller_class_for(:root, context)
			action = 'index'
			
		when 2
			if klass = controller_class_for(parts[1], context)
				# controller/ -> controller.index
				base = "#{@root}/#{parts[1]}"
				action = 'index'
			else
				# action/ -> root.action
				base = @root
				klass = controller_class_for(:root, context)
				action = parts[1]
			end
			
		when 3
			# controller/action/ -> controller.action
			base = "#{@root}/#{parts[1]}"
			klass = controller_class_for(parts[1], context)
			action = parts[2]
	end

	content_type = @apis ? @apis[:api] : 'text/html'

	return klass, "__#{api}__#{action}", base, content_type 
end

#mount(controllers) ⇒ Object

Process the given hash and mount the defined controllers.

Input:

controllers

A hash representing the mapping of mount points to controllers.

Examples

dispatcher.mount { :root => MainController # mounts / ‘users’ => UsersController # mounts /users }



64
65
66
# File 'lib/nitro/dispatcher.rb', line 64

def mount(controllers)
	(@controllers ||= {}).update(controllers)
end