Sha256: 1334bbf937b6833d7093d0f797b54914da53f70da33eb7ff7d8c2f9a6b2f2b4b

Contents?: true

Size: 803 Bytes

Versions: 31

Compression:

Stored size: 803 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.path_to(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::Utils.build_nested_query(query_string_hash))
    end

    final_path
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
rack-app-7.3.2 lib/rack/app/instance_methods/path_to.rb
rack-app-7.3.1 lib/rack/app/instance_methods/path_to.rb
rack-app-7.3.0 lib/rack/app/instance_methods/path_to.rb
rack-app-7.2.2 lib/rack/app/instance_methods/path_to.rb
rack-app-7.2.1 lib/rack/app/instance_methods/path_to.rb
rack-app-7.2.0 lib/rack/app/instance_methods/path_to.rb
rack-app-7.1.0 lib/rack/app/instance_methods/path_to.rb
rack-app-7.0.0 lib/rack/app/instance_methods/path_to.rb
rack-app-6.8.0 lib/rack/app/instance_methods/path_to.rb
rack-app-6.7.1 lib/rack/app/instance_methods/path_to.rb
rack-app-6.7.0 lib/rack/app/instance_methods/path_to.rb