Sha256: 63abb24c19cda24db45c11671b4cf737cfd7552760d00599e4dd31a5c3d3c286

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

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
          raise "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
          raise "item `#{item.relpath}` 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

5 entries across 5 versions & 1 rubygems

Version Path
munge-0.11.1 lib/munge/system/router.rb
munge-0.11.0 lib/munge/system/router.rb
munge-0.10.0 lib/munge/system/router.rb
munge-0.9.0 lib/munge/system/router.rb
munge-0.8.0 lib/munge/system/router.rb