module
Amber::Router::ParamConstraint
Overview
A rule that one route parameter's path segment must pass before its route
can match, such as "digits only" for :id in /posts/:id.
The rule sees the raw segment, before percent-decoding, and judges the
whole segment. The built-in rules in ParamConstraints read each byte
once, so matching takes time linear in the segment's length and uses no
regular-expression engine.
A custom rule is a type that includes this module:
struct EvenNumber
include Amber::Router::ParamConstraint
def matches?(segment : String) : Bool
if number = segment.to_i?
number.even?
else
false
end
end
def to_s(io : IO) : Nil
io << "even_number"
end
end
get "/pages/:number", PagesController, :show, {"number" => EvenNumber.new}
Direct including types
- Amber::Router::ParamConstraints::CharacterRun
- Amber::Router::ParamConstraints::Custom
- Amber::Router::ParamConstraints::OneOf
- Amber::Router::ParamConstraints::Slug
- Amber::Router::ParamConstraints::Uuid
Defined in:
amber/router/param_constraint.crInstance Method Summary
-
#matches?(segment : String) : Bool
Returns
truewhen segment satisfies this rule. -
#matches_span?(path : String, byte_offset : Int32, bytesize : Int32) : Bool
Returns
truewhen the segment of path that starts at byte_offset and is bytesize bytes long satisfies this rule. -
#to_s(io : IO) : Nil
Writes the short label that route listings and
Route#to_jsonshow for this rule.
Instance Method Detail
Returns true when segment satisfies this rule.
Returns true when the segment of path that starts at byte_offset
and is bytesize bytes long satisfies this rule. The router calls this
while it walks a path; the built-in rules override it to read the bytes
in place instead of copying the segment.
Writes the short label that route listings and Route#to_json show for
this rule.