Class: Mustermann::Template

Inherits:
AST::Pattern show all
Defined in:
mustermann-contrib/lib/mustermann/template.rb

Overview

URI template pattern implementation.

Examples:

Mustermann.new('/{foo}') === '/bar' # => true

See Also:

Constant Summary

Constants included from Mustermann

CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError

Instance Attribute Summary

Attributes inherited from RegexpBased

#regexp

Attributes inherited from Pattern

#uri_decode

Instance Method Summary collapse

Methods inherited from AST::Pattern

#expand

Methods inherited from RegexpBased

#initialize, #peek_match, #peek_size

Methods inherited from Pattern

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

#to_templatesArray<String>

Identity patterns support generating templates (the logic is quite complex, though).

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

See Also:


57
58
59
# File 'mustermann-contrib/lib/mustermann/template.rb', line 57

def to_templates
  [to_s]
end