<commit_before>class RouterConstraint
  def params(request)
    request.send(:env)["action_dispatch.request.path_parameters"]
  end

  def set_path_parameters(request, new_params)
    params = params(request)
    controller = params[:controller]
    action     = params[:action]

    params.except!(*params.keys).merge!(
      controller: controller,
      action:     action).merge!(new_params)
  end
end


class FilesRouterConstraint < RouterConstraint
  def matches?(request)
    return false if Repository.find_by_path(request.params[:repository_id]).nil?

    result = !RepositoryFile.find_with_path(
      params_path_without_format(request)).nil?

    set_params_path_without_format(request) if result

    return result
  end

  protected
  def params_path_without_format(request)
    @params = request.send(:env)["action_dispatch.request.path_parameters"]
    @path   = @params[:path]
    @path  += ".#{@params[:format]}" if @params[:format]

    @params.merge({ path: @path }).except(:format)
  end

  def set_params_path_without_format(request)
    params_path_without_format(request)
    @params.merge!({ path: @path }).except!(:format)
  end
end

class LocIdRouterConstraint < RouterConstraint
  def initialize(find_in_klass, **map)
    @find_in_klass = find_in_klass
    @map = map
    super()
  end

  def matches?(request, path = nil)
    path ||= request.original_fullpath
    # retrieves the hierarchy and member portions of loc/id's
    hierarchy_member = path.split('?', 2).first.split('///', 2).first
    element = @find_in_klass.find_with_locid(hierarchy_member)
    ontology = element.respond_to?(:ontology) ? element.ontology : element
    result = !ontology.nil?

    if result
      path_params = {repository_id: ontology.repository.to_param}
      path_params[@map[:ontology]] = ontology.id if @map[:ontology]
      path_params[@map[:element]] = element.id if @map[:element]

      set_path_parameters(request, path_params)
    end

    return result<commit_msg>add/replace to params in loc-id-router constraint<commit_after>class RouterConstraint
  def params(request)
    request.send(:env)["action_dispatch.request.path_parameters"]
  end

  def set_path_parameters(request, new_params)
    params = params(request)
    controller = params[:controller]
    action     = params[:action]

    params.except!(*params.keys).merge!(
      controller: controller,
      action:     action).merge!(new_params)
  end

  def add_path_parameters(request, add_params)
    set_path_parameters(request, params(request).merge(add_params))
  end
end


class FilesRouterConstraint < RouterConstraint
  def matches?(request)
    return false if Repository.find_by_path(request.params[:repository_id]).nil?

    result = !RepositoryFile.find_with_path(
      params_path_without_format(request)).nil?

    set_params_path_without_format(request) if result

    return result
  end

  protected
  def params_path_without_format(request)
    @params = request.send(:env)["action_dispatch.request.path_parameters"]
    @path   = @params[:path]
    @path  += ".#{@params[:format]}" if @params[:format]

    @params.merge({ path: @path }).except(:format)
  end

  def set_params_path_without_format(request)
    params_path_without_format(request)
    @params.merge!({ path: @path }).except!(:format)
  end
end

class LocIdRouterConstraint < RouterConstraint
  def initialize(find_in_klass, **map)
    @find_in_klass = find_in_klass
    @map = map
    super()
  end

  def matches?(request, path = nil)
    path ||= request.original_fullpath
    # retrieves the hierarchy and member portions of loc/id's
    hierarchy_member = path.split('?', 2).first.split('///', 2).first
    element = @find_in_klass.find_with_locid(hierarchy_member)
    ontology = element.respond_to?(:ontology) ? element.ontology : element
    result = !ontology.nil?

    if result
      path_params = {repository_id: ontology.repository.to_param}
      path_params[@map[:ontology]] = ontology.id if @map[:ontology]
      path_params[@map[:element]] = element.id if @map[:element]

      add_path_parameters(request, path_params)
    end

    return result
{'fraction_non_alphanumeric': 0.11508704061895551, 'fraction_numerical': 0.0009671179883945841, 'mean_word_length': 4.216897856242118, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2611779', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1271, 'n_words': 252}