Sha256: 25bc287f8002f793440ad6b4e9d6f2ff3c9abd55470e1c96bcff7308f26c862a

Contents?: true

Size: 1.65 KB

Versions: 38

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

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             = []
        @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

38 entries across 36 versions & 4 rubygems

Version Path
actionpack-7.0.8.7 lib/action_dispatch/journey/routes.rb
actionpack-7.0.8.6 lib/action_dispatch/journey/routes.rb
actionpack-7.0.8.5 lib/action_dispatch/journey/routes.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/journey/routes.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/journey/routes.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/journey/routes.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/journey/routes.rb
actionpack-7.0.8.4 lib/action_dispatch/journey/routes.rb
actionpack-7.0.8.1 lib/action_dispatch/journey/routes.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/routes.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/journey/routes.rb
actionpack-7.0.8 lib/action_dispatch/journey/routes.rb
actionpack-7.0.7.2 lib/action_dispatch/journey/routes.rb
actionpack-7.0.7.1 lib/action_dispatch/journey/routes.rb
actionpack-7.0.7 lib/action_dispatch/journey/routes.rb
actionpack-7.0.6 lib/action_dispatch/journey/routes.rb
actionpack-7.0.5.1 lib/action_dispatch/journey/routes.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/routes.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/journey/routes.rb
actionpack-7.0.5 lib/action_dispatch/journey/routes.rb