Module: Mustermann
- Included in:
- Pattern
- Defined in:
- lib/mustermann.rb,
lib/mustermann/error.rb,
lib/mustermann/caster.rb,
lib/mustermann/mapper.rb,
lib/mustermann/router.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/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/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/ast/transformer.rb,
lib/mustermann/ast/param_scanner.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, 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
Class Method Summary collapse
-
.[](name) ⇒ Class, #new
Maps a type to its factory.
-
.new(*input, type: DEFAULT_TYPE, **options) ⇒ Mustermann::Pattern
Creates a new pattern based on input.
Class Method Details
.[](name) ⇒ Class, #new
Maps a type to its factory.
87 88 89 90 91 92 93 94 95 |
# File 'lib/mustermann.rb', line 87 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.})" if error}" % name } end end end |
.new(*input, type: DEFAULT_TYPE, **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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mustermann.rb', line 60 def self.new(*input, type: DEFAULT_TYPE, **) type ||= DEFAULT_TYPE input = input.first if input.size < 2 case input when Pattern then input when Regexp then self[:regexp].new(input, **) when String then self[type].new(input, **) when Symbol then self[:sinatra].new(input.inspect, **) when Array then Composite.new(input, type: type, **) else pattern = input.to_pattern(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 |