Sha256: e66d229666e5cf337330dc95b72a7045b9126afb04e2833260a7cb512e49b311

Contents?: true

Size: 1.67 KB

Versions: 22

Compression:

Stored size: 1.67 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.ast.grep(Nodes::Symbol).all?(&:default_regexp?)
          anchored_routes << route
        else
          custom_routes << route
        end
      end

      def ast
        @ast ||= begin
          asts = anchored_routes.map(&:ast)
          Nodes::Or.new(asts)
        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

22 entries across 20 versions & 5 rubygems

Version Path
actionpack-6.0.2.2 lib/action_dispatch/journey/routes.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/journey/routes.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/journey/routes.rb
actionpack-6.0.2.1 lib/action_dispatch/journey/routes.rb
actionpack-6.0.2 lib/action_dispatch/journey/routes.rb
actionpack-6.0.2.rc2 lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/action_dispatch/journey/routes.rb
actionpack-6.0.2.rc1 lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/action_dispatch/journey/routes.rb
actionpack-6.0.1 lib/action_dispatch/journey/routes.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
actionpack-6.0.1.rc1 lib/action_dispatch/journey/routes.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/routes.rb
actionpack-6.0.0 lib/action_dispatch/journey/routes.rb