Module: Strelka::App::Routing::ClassMethods

Defined in:
lib/strelka/app/routing.rb

Overview

Class methods to add to classes with routing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#routerclassObject

The name of the routing strategy class to use



120
121
122
# File 'lib/strelka/app/routing.rb', line 120

def routerclass
  @routerclass
end

#routesObject (readonly)

The list of routes to pass to the Router when the application is created



116
117
118
# File 'lib/strelka/app/routing.rb', line 116

def routes
  @routes
end

Instance Method Details

#add_route(verb, pattern, options = {}, &block) ⇒ Object

Define a route method for the specified verb and pattern with the specified options, and the block as its body.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/strelka/app/routing.rb', line 194

def add_route( verb, pattern, options={}, &block )

	# Start the name of the route method with the HTTP verb, then split the
	# route pattern into its components
	methodparts = [ verb.upcase ]
	patternparts = self.split_route_pattern( pattern )
	self.log.debug "Split pattern %p into parts: %p" % [ pattern, patternparts ]

	# Make a method name from the directories and the named captures of the patterns
	# in the route
	patternparts.each do |part|
		if part.respond_to?( :names )
			methodparts << make_route_name( part )
		else
			methodparts << part
		end
	end
	self.log.debug "  route methodname parts are: %p" % [ methodparts ]
	methodname = methodparts.join( '_' )

	# Define the method using the block from the route as its body
	self.log.debug "  adding route method %p for %p route: %p" % [ methodname, verb, block ]
	define_method( methodname, &block )

	# Remove any existing route for the same verb, patternparts, and options
	# (support for overriding inherited routes)
	self.routes.delete_if do |r|
		r[0] == verb && r[1] == patternparts && r[2][:options] == options
	end

	# Now add all the parts to the routes array for the router created by
	# instances
	self.routes << [
		verb,
		patternparts,
		{:action => self.instance_method(methodname), :options => options}
	]
end

#connect(options = {}, &block) ⇒ Object

Define a route for the CONNECT verb.



176
177
178
# File 'lib/strelka/app/routing.rb', line 176

def connect( options={}, &block )
	self.add_route( :CONNECT, '', options, &block )
end

#delete(pattern = '', options = {}, &block) ⇒ Object

Define a route for the DELETE verb and the given pattern.



164
165
166
# File 'lib/strelka/app/routing.rb', line 164

def delete( pattern='', options={}, &block )
	self.add_route( :DELETE, pattern, options, &block )
end

#get(pattern = '', options = {}, &block) ⇒ Object

Define a route for the GET verb and the given pattern.



146
147
148
# File 'lib/strelka/app/routing.rb', line 146

def get( pattern='', options={}, &block )
	self.add_route( :GET, pattern, options, &block )
end

#has_route?(http_verb, path) ⇒ Boolean

Returns true if the app has a route for the specified verb and path.

Returns:

  • (Boolean)


131
132
133
134
# File 'lib/strelka/app/routing.rb', line 131

def has_route?( http_verb, path )
	path_pattern = self.split_route_pattern( path )
	self.routes.find {|tuple| tuple[0] == http_verb && tuple[1] == path_pattern }
end

#inherited(subclass) ⇒ Object

Inheritance hook – inheriting classes inherit their parents’ routes table.



269
270
271
272
273
# File 'lib/strelka/app/routing.rb', line 269

def inherited( subclass )
	super
	subclass.instance_variable_set( :@routerclass, self.routerclass )
	subclass.instance_variable_set( :@routes, self.routes.dup )
end

#make_route_name(pattern) ⇒ Object

Generate a name based on the parts of the given pattern.



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/strelka/app/routing.rb', line 255

def make_route_name( pattern )
	name = '_re_'

	if pattern.names.empty?
		name += ("%s%#x" % [ pattern.class.name, pattern.object_id * 2 ])
	else
		name += pattern.names.join( '_' )
	end

	return name
end

#options(pattern = '', options = {}, &block) ⇒ Object

Define a route for the OPTIONS verb and the given pattern.



140
141
142
# File 'lib/strelka/app/routing.rb', line 140

def options( pattern='', options={}, &block )
	self.add_route( :OPTIONS, pattern, options, &block )
end

#post(pattern = '', options = {}, &block) ⇒ Object

Define a route for the POST verb and the given pattern.



152
153
154
# File 'lib/strelka/app/routing.rb', line 152

def post( pattern='', options={}, &block )
	self.add_route( :POST, pattern, options, &block )
end

#put(pattern = '', options = {}, &block) ⇒ Object

Define a route for the PUT verb and the given pattern.



158
159
160
# File 'lib/strelka/app/routing.rb', line 158

def put( pattern='', options={}, &block )
	self.add_route( :PUT, pattern, options, &block )
end

#route_methodsObject

Return a Hash of the methods defined by routes.



125
126
127
# File 'lib/strelka/app/routing.rb', line 125

def route_methods
	return self.instance_methods.grep( /^#{HTTP::RFC2616_VERB_REGEX}(_|$)/ )
end

#router(newclass = nil) ⇒ Object

Get/set the router class to use for mapping requests to handlers to +newclass.



182
183
184
185
186
187
188
189
# File 'lib/strelka/app/routing.rb', line 182

def router( newclass=nil )
	if newclass
		self.log.info "%p router class set to: %p" % [ self, newclass ]
		self.routerclass = newclass
	end

	return self.routerclass
end

#split_route_pattern(pattern) ⇒ Object

Split the given pattern into its path components and



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/strelka/app/routing.rb', line 235

def split_route_pattern( pattern )
	return [pattern] if pattern.is_a?( Regexp )

	return pattern.split( '/' ).reject( &:empty? ).collect do |component|

		if component.start_with?( ':' )
			self.log.debug "translating parameter component %p to a regexp" % [component]
			raise ScriptError,
				"parameter-based routing not supported without a 'parameters' plugin" unless
				self.respond_to?( :paramvalidator )
			component = component.slice( 1..-1 )
			self.paramvalidator.constraint_regexp_for( component )
		else
			component
		end
	end
end

#trace(pattern = '', options = {}, &block) ⇒ Object

Define a route for the TRACE verb and the given pattern.



170
171
172
# File 'lib/strelka/app/routing.rb', line 170

def trace( pattern='', options={}, &block )
	self.add_route( :TRACE, pattern, options, &block )
end