Sha256: 566a07d8184c110c3b1560dc8d5c0e33bd14d5baea027be1664039e06cf6656c

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require_relative "router/itemish"

module Munge
  module Core
    class Router
      def initialize(alterant:)
        @registries = { route: [], filepath: [] }
        @alterant = alterant
      end

      def register(router)
        case router.type
        when :route
          @registries[:route].push(router)
        when :filepath
          @registries[:filepath].push(router)
        else
          fail "invalid router"
        end
      end

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

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

      private

      def route_mapper(item, method_name, initial_route = nil)
        if !item.route && !initial_route
          fail "item has no route"
        end

        itemish = Itemish.new(item, @alterant)

        @registries[method_name]
          .select { |router| router.type == method_name }
          .inject(initial_route || item.route) do |route, router|
            if router.match?(route, itemish)
              router.call(route, itemish)
            else
              route
            end
          end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
munge-0.5.0 lib/munge/core/router.rb
munge-0.5.0.beta1 lib/munge/core/router.rb