Module: Mustermann

Included in:
Pattern
Defined in:
mustermann/lib/mustermann.rb,
mustermann/lib/mustermann/error.rb,
mustermann/lib/mustermann/caster.rb,
mustermann/lib/mustermann/concat.rb,
mustermann/lib/mustermann/mapper.rb,
mustermann/lib/mustermann/pattern.rb,
mustermann/lib/mustermann/regular.rb,
mustermann/lib/mustermann/sinatra.rb,
mustermann/lib/mustermann/version.rb,
mustermann/lib/mustermann/ast/node.rb,
mustermann/lib/mustermann/expander.rb,
mustermann/lib/mustermann/identity.rb,
mustermann/lib/mustermann/composite.rb,
mustermann/lib/mustermann/extension.rb,
mustermann/lib/mustermann/ast/parser.rb,
mustermann/lib/mustermann/to_pattern.rb,
mustermann/lib/mustermann/ast/pattern.rb,
mustermann-contrib/lib/mustermann/cake.rb,
mustermann/lib/mustermann/ast/compiler.rb,
mustermann/lib/mustermann/ast/expander.rb,
mustermann/lib/mustermann/equality_map.rb,
mustermann/lib/mustermann/regexp_based.rb,
mustermann/lib/mustermann/simple_match.rb,
mustermann-contrib/lib/mustermann/flask.rb,
mustermann-contrib/lib/mustermann/rails.rb,
mustermann-contrib/lib/mustermann/shell.rb,
mustermann/lib/mustermann/pattern_cache.rb,
mustermann-contrib/lib/mustermann/simple.rb,
mustermann/lib/mustermann/ast/boundaries.rb,
mustermann/lib/mustermann/ast/translator.rb,
mustermann/lib/mustermann/ast/validation.rb,
mustermann/lib/mustermann/sinatra/parser.rb,
mustermann-contrib/lib/mustermann/express.rb,
mustermann-contrib/lib/mustermann/pyramid.rb,
mustermann/lib/mustermann/ast/transformer.rb,
mustermann-contrib/lib/mustermann/template.rb,
mustermann-contrib/lib/mustermann/versions.rb,
mustermann/lib/mustermann/ast/param_scanner.rb,
mustermann-contrib/lib/mustermann/file_utils.rb,
mustermann-contrib/lib/mustermann/visualizer.rb,
mustermann/lib/mustermann/sinatra/try_convert.rb,
mustermann/lib/mustermann/sinatra/safe_renderer.rb,
mustermann-contrib/lib/mustermann/string_scanner.rb,
mustermann/lib/mustermann/ast/template_generator.rb,
mustermann-contrib/lib/mustermann/visualizer/tree.rb,
mustermann-contrib/lib/mustermann/visualizer/highlight.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter.rb,
mustermann-contrib/lib/mustermann/file_utils/glob_pattern.rb,
mustermann-contrib/lib/mustermann/visualizer/renderer/ansi.rb,
mustermann-contrib/lib/mustermann/visualizer/renderer/html.rb,
mustermann-contrib/lib/mustermann/visualizer/renderer/sexp.rb,
mustermann-contrib/lib/mustermann/visualizer/tree_renderer.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter/ast.rb,
mustermann-contrib/lib/mustermann/visualizer/renderer/generic.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter/dummy.rb,
mustermann-contrib/lib/mustermann/visualizer/pattern_extension.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter/ad_hoc.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter/regular.rb,
mustermann-contrib/lib/mustermann/visualizer/highlighter/composite.rb,
mustermann-contrib/lib/mustermann/visualizer/renderer/hansi_template.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, Extension, FileUtils, ToPattern, Visualizer Classes: Cake, Composite, Concat, EqualityMap, Expander, Express, Flask, Identity, Mapper, Pattern, PatternCache, Pyramid, Rails, RegexpBased, Regular, Shell, Simple, SimpleMatch, Sinatra, StringScanner, Template

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 'mustermann/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 'mustermann/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