Sha256: ebaaef6a28773011e6c8d5ab78865c47933fa51a182411ed2676f9c291183326

Contents?: true

Size: 1.47 KB

Versions: 27

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
require 'mustermann'

module Mustermann
  # Mixin for adding {#to_pattern} ducktyping to objects.
  #
  # @example
  #   require 'mustermann/to_pattern'
  #
  #   class Foo
  #     include Mustermann::ToPattern
  #
  #     def to_s
  #       ":foo/:bar"
  #     end
  #   end
  #
  #   Foo.new.to_pattern # => #<Mustermann::Sinatra:":foo/:bar">
  #
  # By default included into String, Symbol, Regexp, Array and {Mustermann::Pattern}.
  module ToPattern
    PRIMITIVES = [String, Symbol, Array, Regexp, Mustermann::Pattern]
    private_constant :PRIMITIVES

    # Converts the object into a {Mustermann::Pattern}.
    #
    # @example converting a string
    #   ":name.png".to_pattern # => #<Mustermann::Sinatra:":name.png">
    #
    # @example converting a string with options
    #   "/*path".to_pattern(type: :rails) # => #<Mustermann::Rails:"/*path">
    #
    # @example converting a regexp
    #   /.*/.to_pattern # => #<Mustermann::Regular:".*">
    #
    # @example converting a pattern
    #   Mustermann.new("foo").to_pattern # => #<Mustermann::Sinatra:"foo">
    #
    # @param [Hash] options The options hash.
    # @return [Mustermann::Pattern] pattern corresponding to object.
    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

    PRIMITIVES.each do |klass|
      append_features(klass)
    end
  end
end

Version data entries

27 entries across 24 versions & 5 rubygems

Version Path
mustermann-3.0.3 lib/mustermann/to_pattern.rb
mustermann-3.0.2 lib/mustermann/to_pattern.rb
mustermann-3.0.1 lib/mustermann/to_pattern.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/to_pattern.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/to_pattern.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/to_pattern.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/to_pattern.rb
mustermann-3.0.0 lib/mustermann/to_pattern.rb
mustermann-2.0.2 lib/mustermann/to_pattern.rb
mustermann-2.0.1 lib/mustermann/to_pattern.rb
mustermann-2.0.0 lib/mustermann/to_pattern.rb
mustermann-1.1.2 lib/mustermann/to_pattern.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/to_pattern.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/to_pattern.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/to_pattern.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/to_pattern.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/to_pattern.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/to_pattern.rb
mustermann-1.1.1 lib/mustermann/to_pattern.rb
mustermann-1.1.0 lib/mustermann/to_pattern.rb