<commit_before>require 'action_controller'

# allows to install a filter to the route set by calling: map.filter 'locale'
ActionController::Routing::RouteSet::Mapper.class_eval do
  def filter(*args)
    @set.add_filters(*args)
  end
end

# same here for the optimized url generation in named routes
ActionController::Routing::RouteSet::NamedRouteCollection.class_eval do
  # gosh. monkey engineering optimization code
  def generate_optimisation_block_with_filtering(*args)
    code = generate_optimisation_block_without_filtering(*args)
    if match = code.match(%r(^return (.*) if (.*)))
      # returned string must not contain newlines, or we'll spill out of inline code comments in
      # ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper
      "returning(#{match[1]}) { |result|" +
      "  ActionController::Routing::Routes.filters.run(:around_generate, *args, &lambda{ result }) " +
      "} if #{match[2]}"
    end
  end
  alias_method_chain :generate_optimisation_block, :filtering
end

ActionController::Routing::RouteSet.class_eval do
  attr_writer :filters

  def filters
    @filters ||= RoutingFilter::Chain.new
  end

  def add_filters(*names)<commit_msg>'returning' is deprecated; use 'tap' instead<commit_after>require 'action_controller'

# allows to install a filter to the route set by calling: map.filter 'locale'
ActionController::Routing::RouteSet::Mapper.class_eval do
  def filter(*args)
    @set.add_filters(*args)
  end
end

# same here for the optimized url generation in named routes
ActionController::Routing::RouteSet::NamedRouteCollection.class_eval do
  # gosh. monkey engineering optimization code
  def generate_optimisation_block_with_filtering(*args)
    code = generate_optimisation_block_without_filtering(*args)
    if match = code.match(%r(^return (.*) if (.*)))
      # returned string must not contain newlines, or we'll spill out of inline code comments in
      # ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper
      "#{match[1]}.tap { |result|" +
      "  ActionController::Routing::Routes.filters.run(:around_generate, *args, &lambda{ result }) " +
      "} if #{match[2]}"
    end
  end
  alias_method_chain :generate_optimisation_block, :filtering
end

ActionController::Routing::RouteSet.class_eval do
  attr_writer :filters

  def filters
    @filters ||= RoutingFilter::Chain.new
  end

  def add_filters(*names)
{'fraction_non_alphanumeric': 0.10652353426919901, 'fraction_numerical': 0.0016515276630883566, 'mean_word_length': 5.19693094629156, 'pattern_counts': {'":': 0, '<': 3, '<?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': '396043', 'n_tokens_mistral': 712, 'n_tokens_neox': 702, 'n_words': 180}