class

Amber::Router::Parsers::OptionalSegmentResolver

Inherits Reference < Object

Resolves "optional" segments in urls to many urls.

In this class an optional is a parenthetical statement. For example, these urls all have one or more optional segments:

  • users/:id(.format)
  • relatives(/:id/children)
  • students(/peers(/teachers))

This resolver pays no attention to url structure other than parenthesis. As a result, care should be taken to properly structure optionals so that resolved urls are valid. It is easiest to maintain valid resolved urls by consistently placing delimiters relative to optional boundaries. For example:

All optionals begin with a delimiter: users/:id(/children(/grandchildren))/ All optionals end with a delimiter: users/:id/(children/(grandchildren/))

Both examples produce the same result, which is this set of paths:

[
  "users/:id/",
  "users/:id/children/",
  "users/:id/children/grandchildren/",
]

However, it is unwise to mix styles because it results in incorrect url delimiters.

For example: users/:id(/children/(grandchildren/))/cousins

Produces this set of paths:

[ "users/:id/cousins", "users/:id/children//cousins", "users/:id/children/grandchildren//cousins", ]

Constructors

new(path : String)
Source

Class methods

necessary?(path : String) : Bool
Source
resolve(path : String) : Array(String)
Source

Instance methods

paths
Source
resolve

Iterates the path array until there are no more optionals to resolve, populating the paths array with resolutions.

Source
resolve_optional(path : Array(String)) : Array(Array(String))

Converts a single path with at least one optional into two paths. One with the optional and one without.

When a path has nested optionals, only the outermost optional is resolved.

Source