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

Constant Summary

Constants included from Mustermann

CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError

Instance Attribute Summary

Attributes inherited from RegexpBased

#regexp

Attributes inherited from Pattern

#uri_decode

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RegexpBased

#initialize, #match, #peek_match, #peek_size

Methods inherited from Pattern

#+, #==, #===, #=~, #eql?, #hash, #initialize, #match, 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

Class Method Details

.ast_cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/mustermann/ast/pattern.rb', line 28

def self.ast_cache
  @ast_cache ||= EqualityMap.new
end

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:



122
123
124
125
# File 'lib/mustermann/ast/pattern.rb', line 122

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

#identity_params?(params) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


150
151
152
# File 'lib/mustermann/ast/pattern.rb', line 150

def identity_params?(params)
  param_converters.empty? && super
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:



133
134
135
# File 'lib/mustermann/ast/pattern.rb', line 133

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