Sha256: 80b26acd532dfa6652aa91c85517c90082bce874172a093416369ec91aea046c

Contents?: true

Size: 1.7 KB

Versions: 87

Compression:

Stored size: 1.7 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
        return if ast.nil?
        @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

87 entries across 87 versions & 4 rubygems

Version Path
actionpack-5.2.8.1 lib/action_dispatch/journey/routes.rb
actionpack-5.2.8 lib/action_dispatch/journey/routes.rb
actionpack-5.2.7.1 lib/action_dispatch/journey/routes.rb
actionpack-5.2.7 lib/action_dispatch/journey/routes.rb
actionpack-5.2.6.3 lib/action_dispatch/journey/routes.rb
actionpack-5.2.6.2 lib/action_dispatch/journey/routes.rb
actionpack-5.2.6.1 lib/action_dispatch/journey/routes.rb
actionpack-5.2.6 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.6 lib/action_dispatch/journey/routes.rb
actionpack-5.2.5 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.5 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.4 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.3 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.2 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.1 lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/routes.rb
actionpack-5.2.4 lib/action_dispatch/journey/routes.rb
actionpack-5.2.4.rc1 lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/routes.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/routes.rb