Sha256: a430eb4dd086bcd9a848ca2469af2c4d37280f7dd2dff68e11a3357d76198246

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Hanami
  class Router
    # URL Path prefix
    #
    # @since 2.0.0
    # @api private
    class Prefix
      # @since 2.0.0
      # @api private
      def initialize(prefix)
        @prefix = prefix
      end

      # @since 2.0.0
      # @api private
      def join(path)
        self.class.new(
          _join(path)
        )
      end

      # @since 2.0.0
      # @api private
      def relative_join(path, separator = DEFAULT_SEPARATOR)
        _join(path.to_s)
          .gsub(DEFAULT_SEPARATOR_REGEXP, separator)[1..-1]
      end

      # @since 2.0.0
      # @api private
      def to_s
        @prefix
      end

      # @since 2.0.0
      # @api private
      def to_sym
        @prefix.to_sym
      end

      private

      # @since 2.0.0
      # @api private
      DEFAULT_SEPARATOR = "/"

      # @since 2.0.0
      # @api private
      DEFAULT_SEPARATOR_REGEXP = /\//.freeze

      # @since 2.0.0
      # @api private
      DOUBLE_DEFAULT_SEPARATOR_REGEXP = /[\/]{2,}/.freeze

      # @since 2.0.0
      # @api private
      def _join(path)
        (@prefix + DEFAULT_SEPARATOR + path)
          .gsub(DOUBLE_DEFAULT_SEPARATOR_REGEXP, DEFAULT_SEPARATOR)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-router-2.0.0.alpha2 lib/hanami/router/prefix.rb