Module: Mustermann

Included in:
Pattern
Defined in:
lib/mustermann.rb,
lib/mustermann/error.rb,
lib/mustermann/caster.rb,
lib/mustermann/concat.rb,
lib/mustermann/mapper.rb,
lib/mustermann/pattern.rb,
lib/mustermann/regular.rb,
lib/mustermann/sinatra.rb,
lib/mustermann/version.rb,
lib/mustermann/ast/node.rb,
lib/mustermann/expander.rb,
lib/mustermann/identity.rb,
lib/mustermann/composite.rb,
lib/mustermann/ast/parser.rb,
lib/mustermann/to_pattern.rb,
lib/mustermann/ast/pattern.rb,
lib/mustermann/ast/compiler.rb,
lib/mustermann/ast/expander.rb,
lib/mustermann/equality_map.rb,
lib/mustermann/regexp_based.rb,
lib/mustermann/simple_match.rb,
lib/mustermann/pattern_cache.rb,
lib/mustermann/ast/boundaries.rb,
lib/mustermann/ast/translator.rb,
lib/mustermann/ast/validation.rb,
lib/mustermann/sinatra/parser.rb,
lib/mustermann/ast/transformer.rb,
lib/mustermann/ast/param_scanner.rb,
lib/mustermann/sinatra/try_convert.rb,
lib/mustermann/sinatra/safe_renderer.rb,
lib/mustermann/ast/template_generator.rb

Overview

Namespace and main entry point for the Mustermann library.

Under normal circumstances the only external API entry point you should be using is Mustermann.new.

Defined Under Namespace

Modules: AST, ToPattern Classes: Composite, Concat, EqualityMap, Expander, Identity, Mapper, Pattern, PatternCache, RegexpBased, Regular, SimpleMatch, Sinatra

Constant Summary collapse

DEFAULT_TYPE =

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

Type to use if no type is given.

:sinatra
Error =

Raised if anything goes wrong while generating a Pattern.

Class.new(StandardError)
CompileError =

Raised if anything goes wrong while compiling a Pattern.

Class.new(Error)
ParseError =

Raised if anything goes wrong while parsing a Pattern.

Class.new(Error)
ExpandError =

Raised if anything goes wrong while expanding a Pattern.

Class.new(Error)

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Class, #new

Maps a type to its factory.

Examples:

Mustermann[:sinatra] # => Mustermann::Sinatra

Parameters:

  • name (Symbol)

    a pattern type identifier

Returns:

  • (Class, #new)

    pattern factory

Raises:

  • (ArgumentError)

    if the type is not supported



89
90
91
92
93
94
95
96
97
# File 'lib/mustermann.rb', line 89

def self.[](name)
  return name if name.respond_to? :new
  @types.fetch(normalized = normalized_type(name)) do
    @mutex.synchronize do
      error = try_require "mustermann/#{normalized}"
      @types.fetch(normalized) { raise ArgumentError, "unsupported type %p#{" (#{error.message})" if error}" % name }
    end
  end
end

.new(*input, type: DEFAULT_TYPE, operator: :|, **options) ⇒ Mustermann::Pattern

Creates a new pattern based on input.

  • From Pattern: returns given pattern.

  • From String: creates a pattern from the string, depending on type option (defaults to Sinatra)

  • From Regexp: creates a Regular pattern.

  • From Symbol: creates a Sinatra pattern with a single named capture named after the input.

  • From an Array or multiple inputs: creates a new pattern from each element, combines them to a Composite.

  • From anything else: Will try to call to_pattern on it or raise a TypeError.

Note that if the input is a Pattern, Regexp or Symbol, the type option is ignored and if to_pattern is called on the object, the type will be handed on but might be ignored by the input object.

If you want to enforce the pattern type, you should create them via their expected class.

Examples:

creating patterns

require 'mustermann'

Mustermann.new("/:name")                    # => #<Mustermann::Sinatra:"/example">
Mustermann.new("/{name}", type: :template)  # => #<Mustermann::Template:"/{name}">
Mustermann.new(/.*/)                        # => #<Mustermann::Regular:".*">
Mustermann.new(:name, capture: :word)       # => #<Mustermann::Sinatra:":name">
Mustermann.new("/", "/*.jpg", type: :shell) # => #<Mustermann::Composite:(shell:"/" | shell:"/*.jpg")>

using custom #to_pattern

require 'mustermann'

class MyObject
  def to_pattern(**options)
    Mustermann.new("/:name", **options)
  end
end

Mustermann.new(MyObject.new, type: :rails) # => #<Mustermann::Rails:"/:name">

enforcing type

require 'mustermann/sinatra'

Mustermann::Sinatra.new("/:name")

Parameters:

  • input (String, Pattern, Regexp, Symbol, #to_pattern, Array<String, Pattern, Regexp, Symbol, #to_pattern>)

    The representation of the pattern

  • options (Hash)

    The options hash

Returns:

Raises:

  • (TypeError)

    if the passed object cannot be converted to a pattern

  • (ArgumentError)

    if the type is not supported

  • (ArgumentError)

    if some option is not supported

  • (Mustermann::Error)

    if the pattern can’t be generated from the string

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mustermann.rb', line 62

def self.new(*input, type: DEFAULT_TYPE, operator: :|, **options)
  type ||= DEFAULT_TYPE
  input  = input.first if input.size < 2
  case input
  when Pattern then input
  when Regexp  then self[:regexp].new(input, **options)
  when String  then self[type].new(input, **options)
  when Symbol  then self[:sinatra].new(input.inspect, **options)
  when Array   then input.map { |i| new(i, type: type, **options) }.inject(operator)
  else
    pattern = input.to_pattern(type: type, **options) if input.respond_to? :to_pattern
    raise TypeError, "#{input.class} can't be coerced into Mustermann::Pattern" if pattern.nil?
    pattern
  end
end