Sha256: d03a8eade32976accfedc13ca2a85c0957100e1f6e7327286c06c0ed39ad5c31

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

#:nodoc:
module Clutterbuck; end
#:nodoc:
module Clutterbuck::Router; end

# A route within the application.
#
# Not something you should ever have to deal with yourself directly, it's
# part of the internal plumbing of {Clutterbuck::Router}.
#
class Clutterbuck::Router::Route
	attr_accessor :verb, :path_match

	# @param verb [String]
	#
	# @param path_match [String, Regexp]
	#
	# @param method [UnboundMethod] is a method to call on an instance of the
	#   app class this route is defined on, which will do whatever is needed
	#   to handle the route.  Because you can't just create an
	#   `UnboundMethod` out of thin air, the `UnboundMethod` needs to be
	#   created in the class, and then passed into here.  Ugly.
	#
	def initialize(verb, path_match, method)
		unless path_match.is_a?(String) or path_match.is_a?(Regexp)
			raise ArgumentError,
			      "path must be either a string or a regexp"
		end

		@verb, @path_match, @method = verb, path_match, method
	end

	# Can this route handle a request for the specified path?
	#
	# @param path [String]
	#
	# @return Boolean
	#
	def handles?(path)
		!!case @path_match
		when String
			@path_match == path
		when Regexp
			@path_match =~ path
		end
	end

	# Execute the handler for the route
	#
	# Run the method
	def run(obj, path)
		args = case @path_match
			when String then []
			when Regexp then @path_match.match(path)[1..-1]
		end

		@method.bind(obj).call(*args)
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clutterbuck-router-0.0.2 lib/clutterbuck/router/route.rb
clutterbuck-router-0.0.1 lib/clutterbuck/router/route.rb