Class: Mustermann::AST::Pattern Abstract

Inherits:
RegexpBased show all
Extended by:
Forwardable, SingleForwardable
Defined in:
lib/mustermann/ast/pattern.rb

Overview

This class is abstract.

Superclass for pattern styles that parse an AST from the string pattern.

Direct Known Subclasses

Rails, Sinatra, Template

Instance Attribute Summary

Attributes inherited from RegexpBased

#regexp

Instance Method Summary collapse

Methods inherited from RegexpBased

#initialize

Methods inherited from Pattern

#===, #=~, #initialize, #match, #named_captures, #names, new, #params, supported?, supported_options, #to_s

Constructor Details

This class inherits a constructor from Mustermann::RegexpBased

Instance Method Details

#expand(**values) ⇒ String

All AST-based pattern implementations support expanding.

Examples:

Expanding a pattern

pattern = Mustermann.new('/:name(.:ext)?')
pattern.expand(name: 'hello')             # => "/hello"
pattern.expand(name: 'hello', ext: 'png') # => "/hello.png"

Checking if a pattern supports expanding

if pattern.respond_to? :expand
  pattern.expand(name: "foo")
else
  warn "does not support expanding"
end

Parameters:

  • values (Hash{Symbol: #to_s, Array<#to_s>})

    values to use for expansion

Returns:

  • (String)

    expanded string

Raises:

  • (NotImplementedError)

    raised if expand is not supported.

  • (Mustermann::ExpandError)

    raised if a value is missing or unknown

See Also:



76
77
78
79
# File 'lib/mustermann/ast/pattern.rb', line 76

def expand(**values)
  @expander ||= Mustermann::Expander.new(self)
  @expander.expand(**values)
end