Module: Mustermann

Included in:
Pattern
Defined in:
lib/mustermann.rb,
lib/mustermann/error.rb,
lib/mustermann/flask.rb,
lib/mustermann/grape.rb,
lib/mustermann/rails.rb,
lib/mustermann/shell.rb,
lib/mustermann/caster.rb,
lib/mustermann/mapper.rb,
lib/mustermann/router.rb,
lib/mustermann/simple.rb,
lib/mustermann/express.rb,
lib/mustermann/pattern.rb,
lib/mustermann/pyramid.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/template.rb,
lib/mustermann/composite.rb,
lib/mustermann/extension.rb,
lib/mustermann/ast/parser.rb,
lib/mustermann/to_pattern.rb,
lib/mustermann/ast/pattern.rb,
lib/mustermann/router/rack.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/router/simple.rb,
lib/mustermann/ast/boundaries.rb,
lib/mustermann/ast/translator.rb,
lib/mustermann/ast/validation.rb,
lib/mustermann/rails/versions.rb,
lib/mustermann/string_scanner.rb,
lib/mustermann/ast/transformer.rb,
lib/mustermann/ast/param_scanner.rb,
lib/mustermann/ast/tree_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, Extension, Router, ToPattern Classes: Composite, Expander, Express, Flask, Grape, 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
VERSION =
'0.4.4'

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Class, #new

Maps a type to its factory.

Examples:

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

Raises:

  • (ArgumentError)

    if the type is not supported


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

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) ⇒ 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")

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:


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

def self.new(*input)
  options = input.last.kind_of?(Hash) ? input.pop : {}
  type = options.delete(: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 Array   then Composite.new(input, options.merge(:type => type))
  when Symbol  then self[:sinatra].new(input.inspect, options)
  else
    pattern = input.to_pattern(options.merge(:type => type)) if input.respond_to? :to_pattern
    raise TypeError, "#{input.class} can't be coerced into Mustermann::Pattern" if pattern.nil?
    pattern
  end
end

.register(name, type) ⇒ Object


109
110
111
112
113
114
115
116
# File 'lib/mustermann.rb', line 109

def self.register(*identifiers)
  options  = identifiers.last.is_a?(Hash) ? identifiers.pop : {}
  constant = options[:constant] || identifiers.first.to_s.capitalize
  load     = options[:load] || "mustermann/#{identifiers.first}"
  @register ||= {}
  identifiers.each { |i| @register[i] = [constant, load] }
  @register
end