Class: Mustermann::Sinatra

Inherits:
AST::Pattern show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra/parser.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra/try_convert.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra/safe_renderer.rb

Overview

Sinatra 2.0 style 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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AST::Pattern

#expand, #to_templates

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, #to_templates

Methods included from Mustermann

[], new

Constructor Details

This class inherits a constructor from Mustermann::RegexpBased

Class Method Details

.escape(string) ⇒ String

Takes a string and espaces any characters that have special meaning for Sinatra patterns.

Examples:

require 'mustermann/sinatra'
Mustermann::Sinatra.escape("/:name") # => "/\\:name"

Parameters:

  • string (#to_s)

    the input string

Returns:

  • (String)

    the escaped string



29
30
31
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra.rb', line 29

def self.escape(string)
  string.to_s.gsub(/[\?\(\)\*:\\\|\{\}]/) { |c| "\\#{c}" }
end

Instance Method Details

#safe_stringString

Generates a string represenation of the pattern that can safely be used for def interpolation without changing its semantics.

Examples:

require 'mustermann'
unsafe = Mustermann.new("/:name")

Mustermann.new("#{unsafe}bar").params("/foobar") # => { "namebar" => "foobar" }
Mustermann.new("#{unsafe.safe_string}bar").params("/foobar") # => { "name" => "bar" }

Returns:

  • (String)

    string representatin of the pattern



76
77
78
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra.rb', line 76

def safe_string
  @safe_string ||= SafeRenderer.translate(to_ast)
end

#|(other) ⇒ Mustermann::Pattern

Creates a pattern that matches any string matching either one of the patterns. If a string is supplied, it is treated as a fully escaped Sinatra pattern.

If the other pattern is also a Sintara pattern, it might join the two to a third sinatra pattern instead of generating a composite for efficiency reasons.

This only happens if the sinatra pattern behaves exactly the same as a composite would in regards to matching, parsing, expanding and template generation.

Examples:

pattern = Mustermann.new('/foo/:name') | Mustermann.new('/:first/:second')
pattern === '/foo/bar' # => true
pattern === '/fox/bar' # => true
pattern === '/foo'     # => false

Parameters:

Returns:

See Also:



59
60
61
62
63
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/sinatra.rb', line 59

def |(other)
  return super unless converted = self.class.try_convert(other, **options)
  return super unless converted.names.empty? or names.empty?
  self.class.new(safe_string + "|" + converted.safe_string, **options)
end