Sha256: 8f9ec2f734af41f9d0d5e43085a11062ddf0752ba10fff28af8e927a99b00cfe

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require_relative "router/itemish"

module Munge
  class System
    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

1 entries across 1 versions & 1 rubygems

Version Path
munge-0.6.0 lib/munge/system/router.rb