Sha256: 52ac3939188df9f0392efcf67b9047c8346cee1b94ad0d35b9107231a609279c

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module Munge
  module Router
    class AutoAddExtension
      def initialize(keep_extensions:)
        @keep_extensions = keep_extensions
      end

      def type
        :route
      end

      def match?(initial_route, item)
        item_should_have_extension?(item) && route_doesnt_have_extension?(initial_route)
      end

      def call(initial_route, item)
        add_extension(initial_route, item)
      end

      private

      def add_extension(initial_route, item)
        intersection = item.extensions & @keep_extensions
        extension    = intersection[0]

        "#{initial_route}.#{extension}"
      end

      def item_should_have_extension?(item)
        intersection = item.extensions & @keep_extensions

        intersection.length > 0
      end

      def route_doesnt_have_extension?(initial_route)
        initial_route_extensions = initial_route.split(".")[1..-1]

        intersection = initial_route_extensions & @keep_extensions

        intersection.length == 0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
munge-0.5.0 lib/munge/routers/auto_add_extension.rb
munge-0.5.0.beta1 lib/munge/routers/auto_add_extension.rb