Class: Utopia::Controller::Rewrite::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/controller/rewrite.rb

Overview

Rewrite a request path based on a set of defined rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRewriter

Initialize an empty rewrite rule sequence.



82
83
84
# File 'lib/utopia/controller/rewrite.rb', line 82

def initialize
	@rules = []
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



86
87
88
# File 'lib/utopia/controller/rewrite.rb', line 86

def rules
  @rules
end

Instance Method Details

#apply(context, request, path) ⇒ Object

Apply every rewrite rule in order.



101
102
103
104
105
106
107
# File 'lib/utopia/controller/rewrite.rb', line 101

def apply(context, request, path)
	@rules.each do |rule|
		path = rule.apply(context, request, path)
	end
	
	return path
end

#call(context, request, path) ⇒ Object

Rewrite a path's components in place.



114
115
116
# File 'lib/utopia/controller/rewrite.rb', line 114

def call(context, request, path)
	path.components = apply(context, request, path).components
end

#extract_prefix(**patterns, &block) ⇒ Object

Add a rule that extracts a typed prefix from the request path.



92
93
94
# File 'lib/utopia/controller/rewrite.rb', line 92

def extract_prefix(**patterns, &block)
	@rules << ExtractPrefixRule.new(patterns, block)
end