Class: Hanami::Router::UrlHelpers Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/router/url_helpers.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ UrlHelpers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of UrlHelpers.

Since:

  • 2.0.0



14
15
16
17
18
19
20
# File 'lib/hanami/router/url_helpers.rb', line 14

def initialize(base_url)
  @base_url = URI(base_url)
  @named = {}
  prefix = @base_url.path
  prefix = DEFAULT_PREFIX if prefix.empty?
  @prefix = Prefix.new(prefix)
end

Instance Method Details

#add(name, segment) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



24
25
26
# File 'lib/hanami/router/url_helpers.rb', line 24

def add(name, segment)
  @named[name] = segment
end

#path(name, variables = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



30
31
32
33
34
35
36
# File 'lib/hanami/router/url_helpers.rb', line 30

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

#url(name, variables = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



40
41
42
# File 'lib/hanami/router/url_helpers.rb', line 40

def url(name, variables = {})
  @base_url + @prefix.join(path(name, variables)).to_s
end