Sha256: 2136924119429898604ec55bc33586dfc24fbfb58b671b5da473f59f351a1339

Contents?: true

Size: 887 Bytes

Versions: 8

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

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

module Hanami
  class Router
    # URL Helpers
    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 public
      def path(name, variables = {})
        @named.fetch(name.to_sym) do
          raise InvalidRouteException.new(name)
        end.expand(:append, variables)
      rescue Mustermann::ExpandError => exception
        raise InvalidRouteExpansionException.new(name, exception.message)
      end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hanami-router-2.0.0.beta4 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.beta2 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.beta1 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.alpha6 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.alpha5 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.alpha4 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.alpha3 lib/hanami/router/url_helpers.rb
hanami-router-2.0.0.alpha2 lib/hanami/router/url_helpers.rb