struct Amber::Router::ParamConstraints::CharacterRun

Overview

Matches a segment made only of the given ASCII characters, with a length between #min_length and #max_length bytes.

hex = CharacterRun.new("hex", '0'..'9', 'a'..'f', 'A'..'F')
hex.matches?("c0ffee") # => true
hex.matches?("coffee") # => false

NUMERIC.with_length(3).matches?("042") # => true

Included Modules

Defined in:

amber/router/param_constraints/character_run.cr

Constructors

Instance Method Summary

Instance methods inherited from module Amber::Router::ParamConstraint

matches?(segment : String) : Bool matches?, matches_span?(path : String, byte_offset : Int32, bytesize : Int32) : Bool matches_span?, to_s(io : IO) : Nil to_s

Constructor Detail

def self.new(label : String, *characters : Char | Range(Char, Char), min_length : Int32 = 1, max_length : Int32 | Nil = nil) #

Builds a rule from characters and character ranges. Raises ArgumentError for a character outside ASCII, an empty range, or lengths that no segment could have.


[View source]

Instance Method Detail

def label : String #

[View source]
def matches?(segment : String) : Bool #
Description copied from module Amber::Router::ParamConstraint

Returns true when segment satisfies this rule.


[View source]
def matches_span?(path : String, byte_offset : Int32, bytesize : Int32) : Bool #
Description copied from module Amber::Router::ParamConstraint

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.


[View source]
def max_length : Int32 | Nil #

[View source]
def min_length : Int32 #

[View source]
def to_s(io : IO) : Nil #
Description copied from module Amber::Router::ParamConstraint

Writes the short label that route listings and Route#to_json show for this rule.


[View source]
def with_length(min : Int32, max : Int32 | Nil) : CharacterRun #

Returns a copy of this rule that accepts min to max characters; a nil max leaves the length unbounded.


[View source]
def with_length(length : Int32) : CharacterRun #

Returns a copy of this rule that accepts exactly length characters.


[View source]