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:

  • 0.1.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



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

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:

  • 0.1.0



22
23
24
# File 'lib/hanami/router/url_helpers.rb', line 22

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:

  • 0.1.0



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hanami/router/url_helpers.rb', line 27

def path(name, variables = {})
  scalar_vars = variables.reject { |_, value| value.is_a?(Array) }
  array_vars = array_query_vars(variables)

  expanded_path = @named
    .fetch(name.to_sym) { raise MissingRouteError.new(name) }
    .expand(:append, scalar_vars)

  return expanded_path if array_vars.empty?

  join_char = expanded_path.include?("?") ? "&" : "?"
  "#{expanded_path}#{join_char}#{Rack::Utils.build_query(array_vars)}"
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:

  • 0.1.0



44
45
46
# File 'lib/hanami/router/url_helpers.rb', line 44

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