Sha256: edba91cec80e083d770e08ba02b72852e9f36ff6ae98dd8f97c9692ebd4835f7

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

# frozen_string_literal: true

module Staticky
  class Router
    class Definition
      attr_reader :resources

      def initialize
        @routes_by_path = {}
        @routes_by_component = {}
        @resources = []
      end

      def match(path, to:)
        component = to.then do |object|
          object.is_a?(Class) ? object.new : object
        end

        @resources << resource = Resource.new(url: path, component:)
        @routes_by_path[path] = resource
        @routes_by_component[component.class] = resource
      end

      def root(to:)
        match("/", to:)
      end

      def resolve(path)
        @routes_by_path.fetch(path) { @routes_by_component.fetch(path) }
      end

      def delete(path)
        @routes.delete(path)
      end

      def filepaths
        @resources.map { |resource| rename_key(resource.url) }
      end

      private

      def rename_key(key)
        return "index.html" if key == "/"

        "#{key}.html"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
staticky-0.1.1 lib/staticky/router/definition.rb
staticky-0.1.0 lib/staticky/router/definition.rb