Class: Utopia::Controller::Rewrite::Rewriter
- Inherits:
-
Object
- Object
- Utopia::Controller::Rewrite::Rewriter
- Defined in:
- lib/utopia/controller/rewrite.rb
Overview
Rewrite a request path based on a set of defined rules.
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#apply(context, request, path) ⇒ Object
Apply every rewrite rule in order.
-
#call(context, request, path) ⇒ Object
Rewrite a path's components in place.
-
#extract_prefix(**patterns, &block) ⇒ Object
Add a rule that extracts a typed prefix from the request path.
-
#initialize ⇒ Rewriter
constructor
Initialize an empty rewrite rule sequence.
Constructor Details
#initialize ⇒ Rewriter
Initialize an empty rewrite rule sequence.
80 81 82 |
# File 'lib/utopia/controller/rewrite.rb', line 80 def initialize @rules = [] end |
Instance Attribute Details
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
84 85 86 |
# File 'lib/utopia/controller/rewrite.rb', line 84 def rules @rules end |
Instance Method Details
#apply(context, request, path) ⇒ Object
Apply every rewrite rule in order.
99 100 101 102 103 104 105 |
# File 'lib/utopia/controller/rewrite.rb', line 99 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.
112 113 114 |
# File 'lib/utopia/controller/rewrite.rb', line 112 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.
90 91 92 |
# File 'lib/utopia/controller/rewrite.rb', line 90 def extract_prefix(**patterns, &block) @rules << ExtractPrefixRule.new(patterns, block) end |