Sha256: 71c7e534fc42d489fe022a5f9b9d5b576092d07815de435543714b0f4b86b08a

Contents?: true

Size: 812 Bytes

Versions: 4

Compression:

Stored size: 812 Bytes

Contents

# frozen_string_literal: true
module Rack::App::InstanceMethods::PathTo
  def path_to(defined_path, options = {})
    app_class = options[:class] || self.class

    query_string_hash = options[:params] || {}
    options.each do |k, v|
      k.is_a?(String) && query_string_hash[k] = v
    end

    router = request.env[Rack::App::Constants::ENV::ROUTER]
    final_path = router.final_path_for(app_class, defined_path)

    path_keys = final_path.scan(/:(\w+)/).flatten

    path_keys.each do |key|
      value = query_string_hash.delete(key) || params[key] || raise("missing path-key value for #{key}!")
      final_path.gsub!(/:#{key}/, value.to_s)
    end

    unless query_string_hash.empty?
      final_path.concat("?" + Rack::App::Utils.encode_www_form(query_string_hash))
    end

    final_path
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rack-app-6.2.0 lib/rack/app/instance_methods/path_to.rb
rack-app-6.1.0 lib/rack/app/instance_methods/path_to.rb
rack-app-6.0.0 lib/rack/app/instance_methods/path_to.rb
rack-app-5.12.0 lib/rack/app/instance_methods/path_to.rb