<commit_before>module Munge
  module Core
    class Router
      def initialize(alterant:)
        @registry = []
        @alterant = alterant
      end

      def register(router)
        @registry.push(router)
      end

      def route(item)
        route_mapper(item, :route)
      end

      def filepath(item)
        path = route_mapper(item, :filepath)
        remove_opening_slash(path)
      end

      private

      def route_mapper(item, method_name)
        fail "item has no route" unless item.route
        fail "no routers are registered" if @registry.empty?

        content = @alterant.transform(item)

        @registry
          .select { |router| router.public_methods(false).include?(method_name) }
          .inject(item.route) do |route, router|
            if router.match?(route, content, item)
              router.public_send(method_name, route, content, item)
            else
              route
            end
          end
      end

      def remove_opening_slash(path)
        path.slice(%r([^\/]+.*))
      end
    end
  end<commit_msg>coerce routes to absolute/relative paths<commit_after>module Munge
  module Core
    class Router
      def initialize(alterant:)
        @registry = []
        @alterant = alterant
      end

      def register(router)
        @registry.push(router)
      end

      def route(item)
        path = route_mapper(item, :route)
        Util::Path.ensure_abspath(path)
      end

      def filepath(item)
        path = route_mapper(item, :filepath)
        Util::Path.ensure_relpath(path)
      end

      private

      def route_mapper(item, method_name)
        fail "item has no route" unless item.route
        fail "no routers are registered" if @registry.empty?

        content = @alterant.transform(item)

        @registry
          .select { |router| router.public_methods(false).include?(method_name) }
          .inject(item.route) do |route, router|
            if router.match?(route, content, item)
              router.public_send(method_name, route, content, item)
            else
              route
            end
          end
      end
    end
  end
{'fraction_non_alphanumeric': 0.08768656716417911, 'fraction_numerical': 0.0, 'mean_word_length': 1.8908355795148248, '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': '5252541', 'n_tokens_mistral': 622, 'n_tokens_neox': 605, 'n_words': 151}