class SensibleRouteCollection
An array-like collection of SensibleRoutes
.
Public Class Methods
Initialize a new route collection. For an empty collection, call with no arguments; for a collection built from an existing array of routes, call with the routes: option.
# File lib/sensible_routes.rb, line 76 def initialize(**opts) @routes = if opts[:routes] opts[:routes] else [] end end
Public Instance Methods
Get a route at a particular index from the collection. @param idx the index from which to return an element @return SensibleRoute
# File lib/sensible_routes.rb, line 99 def [](idx) @routes[idx] end
Add a route to the collection. @param new the route to add
# File lib/sensible_routes.rb, line 86 def add(new) @routes << new end
Filter the collection down to only the routes served by the specified controller. @param controller the controller name whose routes should be returned @return SensibleRouteCollection
# File lib/sensible_routes.rb, line 157 def controller(controller) self.class.new(routes: @routes.select { |rt| rt.url_details[:controller] == controller.to_s }) end
Iterate through the collection, performing the specified operation with each element. @param &block a block is required; it will be passed the current element as an argument @return self
# File lib/sensible_routes.rb, line 132 def each @routes.each do |rt| yield rt end self end
Get the first route in the collection. @return SensibleRoute
# File lib/sensible_routes.rb, line 105 def first @routes[0] end
Get the last route in the collection. @return SensibleRoute
# File lib/sensible_routes.rb, line 111 def last @routes[-1] end
Map over the collection, applying the given block as a transformation to each element. @param &block a block to apply to each element; the return value will be included in the returned array @return Array
# File lib/sensible_routes.rb, line 125 def map(&block) @routes.map(&block) end
Find a match from the current route collection for a given path string. Useful for finding the current route being executed. @param path the path string to find a matching route for @return SensibleRoute
# File lib/sensible_routes.rb, line 143 def match_for(path) @routes.select { |rt| rt.match?(path) }.first end
Find the route that responds to the provided url_details (action and controller names). @param url_details the name of a controller and action to find the route for @return SensibleRoute
# File lib/sensible_routes.rb, line 150 def route_for(**url_details) @routes.select { |rt| url_details <= rt.url_details }.first end
Filter the collection to only those entries that match the given block. @param &block a block that returns true to retain an element, or false to reject it @return SensibleRouteCollection
# File lib/sensible_routes.rb, line 118 def select(&block) self.class.new(routes: @routes.select(&block)) end
Get an array representation of the collection. @return Array
# File lib/sensible_routes.rb, line 92 def to_a @routes end
Given a list of path strings, return a new collection with their corresponding routes removed. @param paths a list of path strings to find routes for and remove @return SensibleRouteCollection
# File lib/sensible_routes.rb, line 164 def without(*paths) routes = paths.map { |p| match_for p } self.class.new(routes: @routes - routes) end