Sha256: ab5e9d93071a730641e8907ac50bbae647af62c5af819e4f56979b54ed67adde

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require "hanami/router/redirect"
require "hanami/router/block"

module Hanami
  class Router
    # A route from the router
    #
    # @since 2.0.0
    class Route
      # @api private
      # @since 2.0.0
      ROUTE_CONSTRAINT_SEPARATOR = ", "
      private_constant :ROUTE_CONSTRAINT_SEPARATOR

      # @since 2.0.0
      attr_reader :http_method

      # @since 2.0.0
      attr_reader :path

      # @since 2.0.0
      attr_reader :to

      # @since 2.0.0
      attr_reader :as

      # @since 2.0.0
      attr_reader :constraints

      # @api private
      # @since 2.0.0
      def initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) # rubocop:disable Metrics/ParameterLists
        @http_method = http_method
        @path = path
        @to = to
        @as = as
        @constraints = constraints
        @blk = blk
        freeze
      end

      # @since 2.0.0
      def head?
        http_method == ::Rack::HEAD
      end

      # @since 2.0.0
      def as?
        !as.nil?
      end

      # @since 2.0.0
      def constraints?
        constraints.any?
      end

      # @since 2.0.0
      def inspect_to(value = to)
        case value
        when String
          value
        when Proc
          "(proc)"
        when Class
          value.name || "(class)"
        when Block
          "(block)"
        when Redirect
          "#{value.destination} (HTTP #{to.code})"
        else
          inspect_to(value.class)
        end
      end

      # @since 2.0.0
      def inspect_constraints
        @constraints.map do |key, value|
          "#{key}: #{value.inspect}"
        end.join(ROUTE_CONSTRAINT_SEPARATOR)
      end

      # @since 2.0.0
      def inspect_as
        as ? as.inspect : Router::EMPTY_STRING
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanami-router-2.0.0.beta4 lib/hanami/router/route.rb
hanami-router-2.0.0.beta2 lib/hanami/router/route.rb
hanami-router-2.0.0.beta1 lib/hanami/router/route.rb