Sha256: 485b0ea721bcfa5f7edc87091ad2639aaf3c1c3039b53b309ea786e3d1d4c5a6

Contents?: true

Size: 902 Bytes

Versions: 4

Compression:

Stored size: 902 Bytes

Contents

# frozen_string_literal: true

require "hanami/router/errors"
require "mustermann/error"

module Hanami
  class Router
    # @since 2.0.0
    # @api private
    class UrlHelpers
      # @since 2.0.0
      # @api private
      def initialize(base_url)
        @base_url = base_url
        @named = {}
      end

      # @since 2.0.0
      # @api private
      def add(name, segment)
        @named[name] = segment
      end

      # @since 2.0.0
      # @api private
      def path(name, variables = {})
        @named.fetch(name.to_sym) do
          raise MissingRouteError.new(name)
        end.expand(:append, variables)
      rescue Mustermann::ExpandError => exception
        raise InvalidRouteExpansionError.new(name, exception.message)
      end

      # @since 2.0.0
      # @api private
      def url(name, variables = {})
        @base_url + path(name, variables)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hanami-router-2.0.2 lib/hanami/router/url_helpers.rb
hanami-router-2.0.1 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.rc1 lib/hanami/router/url_helpers.rb