class RestlessRouter::Routes
Constants
- ExistingRouteError
- InvalidRouteError
Error Definitions
- RouteNotFoundError
Public Class Methods
Creates a new instance of a Route
collection. This allows us to keep all definitions in a single object and then later retrieve them by their link relationship name.
@example
routes = RestlessRouter::Routers.new routes.add_route(RestlessRouter::Route.new('home', 'https://example.com/home') routes.route_for('home').url_for # => 'https://example.com/home'
# File lib/restless_router/routes.rb, line 23 def initialize @routes = [] end
Public Instance Methods
Add a new route to the Routes
collection
@return [Array] Routes
collection
# File lib/restless_router/routes.rb, line 36 def add_route(route) raise InvalidRouteError.new('Route must respond to #url_for') unless valid_route?(route) @routes << route unless route_exists?(route) end
Raise an exception if the route is invalid or already exists
# File lib/restless_router/routes.rb, line 44 def add_route!(route) # Raise exception if the route is existing, too raise InvalidRouteError.new('Route must respond to #url_for') unless valid_route?(route) raise ExistingRouteError.new(("Route already exists for %s" % [route.name])) if route_exists?(route) @routes << route end
Define each for use with Enumerable
# File lib/restless_router/routes.rb, line 29 def each(&block) @routes.each(&block) end
Retrieve a route by it’s link relationship name
@return [Route, nil] Instance of the route by name or nil
# File lib/restless_router/routes.rb, line 55 def route_for(name) name = name.to_s @routes.select { |entry| entry.name == name }.first end
Raise an exception of the route’s not found
# File lib/restless_router/routes.rb, line 63 def route_for!(name) route = route_for(name) raise RouteNotFoundError.new(("Route not found for %s" % [name])) if route.nil? route end
Query method to check if any routes have been defined.
@return [Boolean] True if there are definitions in the collection
# File lib/restless_router/routes.rb, line 79 def routes? @routes.any? end
Private Instance Methods
Query method to see if the specified route already exists.
@return [Boolean] True if we already have an existing route
# File lib/restless_router/routes.rb, line 87 def route_exists?(route) !!self.route_for(route.name) end
Query method to see if the route definition being added is valid.
It must respond to:
-
‘name`
-
‘url_for`
@return [Boolean] True if the route definition is valid
# File lib/restless_router/routes.rb, line 99 def valid_route?(route) route.respond_to?(:url_for) && route.respond_to?(:name) end