Class: Hanami::Router::UrlHelpers Private
- Inherits:
-
Object
- Object
- Hanami::Router::UrlHelpers
- 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.
Instance Method Summary collapse
- #add(name, segment) ⇒ Object private
-
#initialize(base_url) ⇒ UrlHelpers
constructor
private
A new instance of UrlHelpers.
- #path(name, variables = {}) ⇒ Object private
- #url(name, variables = {}) ⇒ Object private
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.
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.
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.
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) = @named .fetch(name.to_sym) { raise MissingRouteError.new(name) } .(:append, scalar_vars) return if array_vars.empty? join_char = .include?("?") ? "&" : "?" "#{}#{join_char}#{Rack::Utils.build_query(array_vars)}" rescue Mustermann::ExpandError => exception raise InvalidRouteExpansionError.new(name, exception.) 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.
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 |