Module: Mustermann::ToPattern

Defined in:
mustermann/lib/mustermann/to_pattern.rb

Overview

Mixin for adding #to_pattern ducktyping to objects.

By default included into String, Symbol, Regexp, Array and Pattern.

Examples:

require 'mustermann/to_pattern'

class Foo
  include Mustermann::ToPattern

  def to_s
    ":foo/:bar"
  end
end

Foo.new.to_pattern # => #<Mustermann::Sinatra:":foo/:bar">

Instance Method Summary collapse

Instance Method Details

#to_pattern(**options) ⇒ Mustermann::Pattern

Converts the object into a Pattern.

Examples:

converting a string

":name.png".to_pattern # => #<Mustermann::Sinatra:":name.png">

converting a string with options

"/*path".to_pattern(type: :rails) # => #<Mustermann::Rails:"/*path">

converting a regexp

/.*/.to_pattern # => #<Mustermann::Regular:".*">

converting a pattern

Mustermann.new("foo").to_pattern # => #<Mustermann::Sinatra:"foo">

Parameters:

  • options (Hash)

    The options hash.

Returns:


41
42
43
44
45
# File 'mustermann/lib/mustermann/to_pattern.rb', line 41

def to_pattern(**options)
  input   = self if PRIMITIVES.any? { |p| self.is_a? p }
  input ||= __getobj__ if respond_to?(:__getobj__)
  Mustermann.new(input || to_s, **options)
end