Sha256: 4ed3358ee4a89502ba407df42c3d5ce59203c925e28335cfe6e6bbc2b5b03122

Contents?: true

Size: 1.69 KB

Versions: 23

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

# :markup: markdown

module ActionDispatch
  module Journey # :nodoc:
    # The Routing table. Contains all routes for a system. Routes can be added to
    # the table by calling Routes#add_route.
    class Routes # :nodoc:
      include Enumerable

      attr_reader :routes, :custom_routes, :anchored_routes

      def initialize(routes = [])
        @routes             = routes
        @ast                = nil
        @anchored_routes    = []
        @custom_routes      = []
        @simulator          = nil
      end

      def empty?
        routes.empty?
      end

      def length
        routes.length
      end
      alias :size :length

      def last
        routes.last
      end

      def each(&block)
        routes.each(&block)
      end

      def clear
        routes.clear
        anchored_routes.clear
        custom_routes.clear
      end

      def partition_route(route)
        if route.path.anchored && route.path.requirements_anchored?
          anchored_routes << route
        else
          custom_routes << route
        end
      end

      def ast
        @ast ||= begin
          nodes = anchored_routes.map(&:ast)
          Nodes::Or.new(nodes)
        end
      end

      def simulator
        @simulator ||= begin
          gtg = GTG::Builder.new(ast).transition_table
          GTG::Simulator.new(gtg)
        end
      end

      def add_route(name, mapping)
        route = mapping.make_route name, routes.length
        routes << route
        partition_route(route)
        clear_cache!
        route
      end

      private
        def clear_cache!
          @ast                = nil
          @simulator          = nil
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
actionpack-8.0.1 lib/action_dispatch/journey/routes.rb
actionpack-8.0.0.1 lib/action_dispatch/journey/routes.rb
actionpack-7.2.2.1 lib/action_dispatch/journey/routes.rb
actionpack-8.0.0 lib/action_dispatch/journey/routes.rb
actionpack-7.2.2 lib/action_dispatch/journey/routes.rb
actionpack-8.0.0.rc2 lib/action_dispatch/journey/routes.rb
actionpack-7.2.1.2 lib/action_dispatch/journey/routes.rb
actionpack-8.0.0.rc1 lib/action_dispatch/journey/routes.rb
actionpack-7.2.1.1 lib/action_dispatch/journey/routes.rb
actionpack-8.0.0.beta1 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha9 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha8 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha7 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha4 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha3 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha2 lib/action_dispatch/journey/routes.rb
omg-actionpack-8.0.0.alpha1 lib/action_dispatch/journey/routes.rb
actionpack-7.2.1 lib/action_dispatch/journey/routes.rb
actionpack-7.2.0 lib/action_dispatch/journey/routes.rb
actionpack-7.2.0.rc1 lib/action_dispatch/journey/routes.rb