Sha256: eca3bed01d07682bac4f92a3cf20ff614f99aac182623c05eb4f513f858d3514

Contents?: true

Size: 1.24 KB

Versions: 17

Compression:

Stored size: 1.24 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..]
      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 = /\//

      # @since 2.0.0
      # @api private
      DOUBLE_DEFAULT_SEPARATOR_REGEXP = /\/{2,}/

      # @since 2.0.0
      # @api private
      def _join(path)
        return @prefix if path == DEFAULT_SEPARATOR

        (@prefix + DEFAULT_SEPARATOR + path)
          .gsub(DOUBLE_DEFAULT_SEPARATOR_REGEXP, DEFAULT_SEPARATOR)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
hanami-router-2.2.0 lib/hanami/router/prefix.rb
hanami-router-2.2.0.rc1 lib/hanami/router/prefix.rb
hanami-router-2.2.0.beta2 lib/hanami/router/prefix.rb
hanami-router-2.2.0.beta1 lib/hanami/router/prefix.rb
hanami-router-2.1.0 lib/hanami/router/prefix.rb
hanami-router-2.1.0.rc3 lib/hanami/router/prefix.rb
hanami-router-2.1.0.rc2 lib/hanami/router/prefix.rb
hanami-router-2.1.0.rc1 lib/hanami/router/prefix.rb
hanami-router-2.1.0.beta1 lib/hanami/router/prefix.rb
hanami-router-2.0.2 lib/hanami/router/prefix.rb
hanami-router-2.0.1 lib/hanami/router/prefix.rb
hanami-router-2.0.0 lib/hanami/router/prefix.rb
hanami-router-2.0.0.rc1 lib/hanami/router/prefix.rb
hanami-router-2.0.0.beta4 lib/hanami/router/prefix.rb
hanami-router-2.0.0.beta2 lib/hanami/router/prefix.rb
hanami-router-2.0.0.beta1 lib/hanami/router/prefix.rb
hanami-router-2.0.0.alpha6 lib/hanami/router/prefix.rb