-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
url_helpers.rb
41 lines (36 loc) · 902 Bytes
/
url_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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