Class: Mustermann::AST::Pattern Abstract

Inherits:
RegexpBased show all
Extended by:
Forwardable, SingleForwardable
Defined in:
mustermann/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

Cake, Express, Flask, Pyramid, Rails, Sinatra, Template

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, #params, #peek_match, #peek_size

Methods inherited from Pattern

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

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 'mustermann/lib/mustermann/ast/pattern.rb', line 28

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

Instance Method Details

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

    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:



125
126
127
128
# File 'mustermann/lib/mustermann/ast/pattern.rb', line 125

def expand(behavior = nil, values = EMPTY_HASH)
  @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)


154
155
156
# File 'mustermann/lib/mustermann/ast/pattern.rb', line 154

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:



136
137
138
# File 'mustermann/lib/mustermann/ast/pattern.rb', line 136

def to_templates
  @to_templates ||= Mustermann.dedup(generate_templates(to_ast))
end