UtopiaSourceUtopiaControllerRewriteExtractPrefixRule

class ExtractPrefixRule

A rule which extracts a prefix pattern from the request path.

Definitions

def initialize(patterns, block)

Initialize a typed prefix-extraction rule.

Signature

parameter patterns Hash

The path rewrite patterns.

parameter block Proc

The block.

Implementation

def initialize(patterns, block)
	@matcher = Path::Matcher.new(patterns)
	@block = block
end

def freeze

Freeze this object and its internal state.

Signature

returns self

This object.

Implementation

def freeze
	@matcher.freeze
	@block.freeze
	
	super
end

def apply(context, request, path)

Apply this prefix rule and execute its callback when it matches.

Signature

parameter context Object

The context.

parameter request Rack::Request

The request.

parameter path Utopia::Path | String

The path.

returns Path

The unmatched suffix, or the original path when the rule does not match.

Implementation

def apply(context, request, path)
	if match_data = @matcher.match(path)
		apply_match_to_context(match_data, context)
		
		if @block
			context.instance_exec(request, path, match_data, &@block)
		end
		
		return match_data.post_match
	else
		return path
	end
end