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

Sinatra

Constant Summary

Constants included from Mustermann

DEFAULT_TYPE

Instance Attribute Summary

Attributes inherited from RegexpBased

#regexp

Instance Method Summary collapse

Methods inherited from RegexpBased

#initialize, #peek_match, #peek_size

Methods inherited from Pattern

#===, #=~, #initialize, #match, #named_captures, #names, new, #params, #peek, #peek_match, #peek_params, #peek_size, supported?, supported_options, #to_proc, #to_s, #|

Methods included from Mustermann

[], new

Constructor Details

This class inherits a constructor from Mustermann::RegexpBased

Instance Method Details

#expand(behavior = nil, 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:

  • behavior (Symbol) (defaults to: nil)

    What to do with additional key/value pairs not present in the values hash. Possible options: :raise, :ignore, :append.

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

    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:



106
107
108
109
# File 'lib/mustermann/ast/pattern.rb', line 106

def expand(behavior = nil, values = {})
  @expander ||= Mustermann::Expander.new(self)
  @expander.expand(behavior, values)
end

#to_templatesArray<String>

All AST-based pattern implementations support generating templates.

Examples:

generating templates

Mustermann.new("/:name").to_templates                   # => ["/{name}"]
Mustermann.new("/:foo(@:bar)?/*baz").to_templates       # => ["/{foo}@{bar}/{+baz}", "/{foo}/{+baz}"]
Mustermann.new("/{name}", type: :template).to_templates # => ["/{name}"]

generating templates from composite patterns

pattern  = Mustermann.new('/:name')
pattern |= Mustermann.new('/{name}', type: :template)
pattern |= Mustermann.new('/example/*nested')
pattern.to_templates # => ["/{name}", "/example/{+nested}"]

Checking if a pattern supports expanding

if pattern.respond_to? :to_templates
  pattern.to_templates
else
  warn "does not support template generation"
end

Returns:

  • (Array<String>)

    list of URI templates

See Also:



117
118
119
# File 'lib/mustermann/ast/pattern.rb', line 117

def to_templates
  @to_templates ||= generate_templates(to_ast)
end