class ActionDispatch::Journey::Formatter

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/action_dispatch/journey/formatter.rbs

class ActionDispatch::Journey::Formatter
  def extract_parameterized_parts: (ActionDispatch::Journey::Route route, Hash options, Hash recall) -> Hash
  def generate: (String name, Hash options, Hash path_parameters) -> ActionDispatch::Journey::Formatter::RouteWithParams
  def match_route: (String name, Hash options) -> nil
  def missing_keys: (ActionDispatch::Journey::Route route, Hash parts) -> nil
  def named_routes: () -> ActionDispatch::Routing::RouteSet::NamedRouteCollection
end

passed to url_for in Rails will eventually call Formatter#generate.
The Formatter class is used for formatting URLs. For example, parameters

def build_cache

def build_cache
  root = { ___routes: [] }
  routes.routes.each_with_index do |route, i|
    leaf = route.required_defaults.inject(root) do |h, tuple|
      h[tuple] ||= {}
    end
    (leaf[:___routes] ||= []) << [i, route]
  end
  root
end

def cache

def cache
  @cache ||= build_cache
end

def clear

def clear
  @cache = nil
end

def extract_parameterized_parts(route, options, recall)

Experimental RBS support (using type sampling data from the type_fusion project).

type ActionDispatch__Journey__Formatter_extract_parameterized_parts_options = controller | String | action | String | gem | String | version | String | class | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | file | String | controller | String | action | String | id | Trade | controller | String | action | String | id | Trade | via_view | String | controller | String | action | String | gem | String | version | String | class | String | name | String | controller | String | action | String | gem | String | version | String | name | String | controller | String | action | String | gem | String | version | Gem::Version | controller | String | action | String | gem | String | version | String | guide | String | id | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | class_id | String | id | String | controller | String | action | String | gem | String | version | String | id | ModuleDefinition | controller | String | action | String | gem | String | version | String | id | String
type ActionDispatch__Journey__Formatter_extract_parameterized_parts_recall =  | controller | String | action | String | gem | String | version | String | class | String | controller | String | action | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | id | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | id | String

def extract_parameterized_parts: (ActionDispatch::Journey::Route route, ActionDispatch__Journey__Formatter_extract_parameterized_parts_options options, ActionDispatch__Journey__Formatter_extract_parameterized_parts_recall recall) -> (gem | String | version | String | gem | String | version | String | class_id | String | id | String | gem | String | version | String | id | String)

This signature was generated using 80 samples from 2 applications.

def extract_parameterized_parts(route, options, recall)
  parameterized_parts = recall.merge(options)
  keys_to_keep = route.parts.reverse_each.drop_while { |part|
    !(options.key?(part) || route.scope_options.key?(part)) || (options[part].nil? && recall[part].nil?)
  } | route.required_parts
  parameterized_parts.delete_if do |bad_key, _|
    !keys_to_keep.include?(bad_key)
  end
  parameterized_parts.each do |k, v|
    if k == :controller
      parameterized_parts[k] = v
    else
      parameterized_parts[k] = v.to_param
    end
  end
  parameterized_parts.compact!
  parameterized_parts
end

def generate(name, options, path_parameters)

Experimental RBS support (using type sampling data from the type_fusion project).

type ActionDispatch__Journey__Formatter_generate_options = controller | String | action | String | gem | String | version | Gem::Version | controller | String | action | String | gem | String | version | String | module | String | name | String | controller | String | action | String | gem | String | version | String |  | controller | String | action | String | gem | String | version | String | class | String | controller | String | action | String | gem | String | version | String | module | ModuleDefinition | controller | String | action | String | gem | String | version | String | file | String | controller | String | action | String | gem | String | version | String | guide | String | controller | String | action | String | gem | String | version | String | class_id | String | id | String | id | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | id | ModuleDefinition | controller | String | action | String | gem | String | format | String | controller | String | action | String | gem | String | version | String | id | String
type ActionDispatch__Journey__Formatter_generate_path_parameters = controller | String | action | String |  | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | class | String | name | String | controller | String | action | String | gem | String | version | String | guide | String | controller | String | action | String | gem | String | version | String | module | String | controller | String | action | String | gem | String | version | String | guide | String | format | String | controller | String | action | String | gem | String | version | String | id | String | controller | String | action | String | gem | String | version | String | module_id | String | id | String

def generate: (String name, ActionDispatch__Journey__Formatter_generate_options options, ActionDispatch__Journey__Formatter_generate_path_parameters path_parameters) -> ActionDispatch::Journey::Formatter::RouteWithParams

This signature was generated using 70 samples from 2 applications.

def generate(name, options, path_parameters)
  constraints = path_parameters.merge(options)
  missing_keys = nil
  match_route(name, constraints) do |route|
    parameterized_parts = extract_parameterized_parts(route, options, path_parameters)
    # Skip this route unless a name has been provided or it is a
    # standard Rails route since we can't determine whether an options
    # hash passed to url_for matches a Rack application or a redirect.
    next unless name || route.dispatcher?
    missing_keys = missing_keys(route, parameterized_parts)
    next if missing_keys && !missing_keys.empty?
    params = options.dup.delete_if do |key, _|
      parameterized_parts.key?(key) || route.defaults.key?(key)
    end
    defaults       = route.defaults
    required_parts = route.required_parts
    route.parts.reverse_each do |key|
      break if defaults[key].nil? && parameterized_parts[key].present?
      next if parameterized_parts[key].to_s != defaults[key].to_s
      break if required_parts.include?(key)
      parameterized_parts.delete(key)
    end
    return RouteWithParams.new(route, parameterized_parts, params)
  end
  unmatched_keys = (missing_keys || []) & constraints.keys
  missing_keys = (missing_keys || []) - unmatched_keys
  MissingRoute.new(constraints, missing_keys, unmatched_keys, routes, name)
end

def initialize(routes)

def initialize(routes)
  @routes = routes
  @cache  = nil
end

def match_route(name, options)

Experimental RBS support (using type sampling data from the type_fusion project).

type ActionDispatch__Journey__Formatter_match_route_options = controller | String | action | String | gem | String | version | String | class | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | module | String | name | String | controller | String | action | String | id | Company | controller | String | action | String | controller | String | action | String | gem | String | version | String | class | String | name | String | controller | String | action | String | gem | String | version | String | module | ModuleDefinition | controller | String | action | String | gem | String | version | Gem::Version | controller | String | action | String | project | Project | controller | String | action | String | gem | String | version | String | guide | String | controller | String | action | String | gem | String | version | String | module | String | guide | String | controller | String | action | String | gem | String | version | String | module | String | controller | String | action | String | gem | String | version | String | file | String | controller | String | action | String | gem | String | version | String | id | ModuleDefinition | controller | String | action | String | gem | String | version | String | module_id | String | id | String | controller | String | action | String | gem | String | version | String | class_id | String | id | String | id | String | controller | String | action | String | gem | String | version | String | controller | String | action | String | gem | String | version | String | id | String

def match_route: (String name, ActionDispatch__Journey__Formatter_match_route_options options) -> nil

This signature was generated using 86 samples from 2 applications.

def match_route(name, options)
  if named_routes.key?(name)
    yield named_routes[name]
  else
    routes = non_recursive(cache, options)
    supplied_keys = options.each_with_object({}) do |(k, v), h|
      h[k.to_s] = true if v
    end
    hash = routes.group_by { |_, r| r.score(supplied_keys) }
    hash.keys.sort.reverse_each do |score|
      break if score < 0
      hash[score].sort_by { |i, _| i }.each do |_, route|
        yield route
      end
    end
  end
end

def missing_keys(route, parts)

Experimental RBS support (using type sampling data from the type_fusion project).

type ActionDispatch__Journey__Formatter_missing_keys_parts = gem | String | version | String | file | String | gem | String | version | String | class | String |  | gem | String | version | String | class | String | name | String | gem | String | version | String | gem | String | version | String | guide | String | gem | String | version | String | module | String | gem | String | version | String | class_id | String | id | String | gem | String | version | String | id | String | gem | String | version | String | module_id | String | id | String

def missing_keys: (ActionDispatch::Journey::Route route, ActionDispatch__Journey__Formatter_missing_keys_parts parts) -> nil

This signature was generated using 45 samples from 2 applications.

Returns an array populated with missing keys if any are present.
def missing_keys(route, parts)
  missing_keys = nil
  tests = route.path.requirements_for_missing_keys_check
  route.required_parts.each { |key|
    case tests[key]
    when nil
      unless parts[key]
        missing_keys ||= []
        missing_keys << key
      end
    else
      unless tests[key].match?(parts[key])
        missing_keys ||= []
        missing_keys << key
      end
    end
  }
  missing_keys
end

def named_routes

Experimental RBS support (using type sampling data from the type_fusion project).

def named_routes: () -> ActionDispatch::Routing::RouteSet::NamedRouteCollection

This signature was generated using 129 samples from 1 application.

def named_routes
  routes.named_routes
end

def non_recursive(cache, options)

def non_recursive(cache, options)
  routes = []
  queue  = [cache]
  while queue.any?
    c = queue.shift
    routes.concat(c[:___routes]) if c.key?(:___routes)
    options.each do |pair|
      queue << c[pair] if c.key?(pair)
    end
  end
  routes
end

def possibles(cache, options, depth = 0)

def possibles(cache, options, depth = 0)
  cache.fetch(:___routes) { [] } + options.find_all { |pair|
    cache.key?(pair)
  }.flat_map { |pair|
    possibles(cache[pair], options, depth + 1)
  }
end