class RestlessRouter::Routes

Constants

ExistingRouteError
InvalidRouteError

Error Definitions

RouteNotFoundError

Public Class Methods

new() click to toggle source

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

<<(route)
Alias for: add_route
add_route(route) click to toggle source

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
Also aliased as: <<
add_route!(route) click to toggle source

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
each(&block) click to toggle source

Define each for use with Enumerable

# File lib/restless_router/routes.rb, line 29
def each(&block)
  @routes.each(&block)
end
route_for(name) click to toggle source

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
route_for!(name) click to toggle source

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
routes() click to toggle source

Returns the collection of Route definitions

@return [Array] Routes

# File lib/restless_router/routes.rb, line 72
def routes
  @routes
end
routes?() click to toggle source

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

route_exists?(route) click to toggle source

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
valid_route?(route) click to toggle source

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