Sha256: 0fb6eba9775105617367110bca5f5dc961404b54e9b624d04548b0e2d2c09821

Contents?: true

Size: 1.78 KB

Versions: 49

Compression:

Stored size: 1.78 KB

Contents

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, :named_routes

      def initialize
        @routes             = []
        @named_routes       = {}
        @ast                = nil
        @partitioned_routes = nil
        @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
        named_routes.clear
      end

      def partitioned_routes
        @partitioned_routes ||= routes.partition do |r|
          r.path.anchored && r.ast.grep(Nodes::Symbol).all?(&:default_regexp?)
        end
      end

      def ast
        @ast ||= begin
          asts = partitioned_routes.first.map(&:ast)
          Nodes::Or.new(asts) unless asts.empty?
        end
      end

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

      # Add a route to the routing table.
      def add_route(app, path, conditions, defaults, name = nil)
        route = Route.new(name, app, path, conditions, defaults)

        route.precedence = routes.length
        routes << route
        named_routes[name] = route if name && !named_routes[name]
        clear_cache!
        route
      end

      private

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

Version data entries

49 entries across 46 versions & 6 rubygems

Version Path
actionpack-4.2.11.3 lib/action_dispatch/journey/routes.rb
actionpack-4.2.11.2 lib/action_dispatch/journey/routes.rb
actionpack-4.2.11.1 lib/action_dispatch/journey/routes.rb
actionpack-4.2.11 lib/action_dispatch/journey/routes.rb
actionpack-4.2.10 lib/action_dispatch/journey/routes.rb
actionpack-4.2.10.rc1 lib/action_dispatch/journey/routes.rb
actionpack-4.2.9 lib/action_dispatch/journey/routes.rb
actionpack-4.2.9.rc2 lib/action_dispatch/journey/routes.rb
actionpack-4.2.9.rc1 lib/action_dispatch/journey/routes.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionpack-4.2.8/lib/action_dispatch/journey/routes.rb
actionpack-4.2.8 lib/action_dispatch/journey/routes.rb
actionpack-4.2.8.rc1 lib/action_dispatch/journey/routes.rb
actionpack-4.2.7.1 lib/action_dispatch/journey/routes.rb
actionpack-4.2.7 lib/action_dispatch/journey/routes.rb
actionpack-4.1.16 lib/action_dispatch/journey/routes.rb
actionpack-4.1.16.rc1 lib/action_dispatch/journey/routes.rb
actionpack-4.2.7.rc1 lib/action_dispatch/journey/routes.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/journey/routes.rb
actionpack-4.1.15 lib/action_dispatch/journey/routes.rb
actionpack-4.2.6 lib/action_dispatch/journey/routes.rb