Sha256: 0929a791dbd1700a80b94ddde8566915dfe8af3eccc5abe95315c8d833cd023d

Contents?: true

Size: 1014 Bytes

Versions: 1

Compression:

Stored size: 1014 Bytes

Contents

module Munge
  module Routers
    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.empty?
      end

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

        intersection = initial_route_extensions & @keep_extensions

        intersection.empty?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
munge-0.18.0 lib/munge/routers/auto_add_extension.rb