Class: Lennarb::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/routes.rb

Overview

Routes class for managing application routes

Instance Method Summary collapse

Constructor Details

#initializeRoutes

Initialize a new Routes instance



8
9
10
11
# File 'lib/lennarb/routes.rb', line 8

def initialize
  @store = RouteNode.new
  @frozen = false
end

Instance Method Details

#freezeself

Freeze the routes to prevent further modification.

Freezes the whole tree, not only the root, so a frozen copy really is immutable.

Returns:

  • (self)

    The frozen routes



57
58
59
60
61
# File 'lib/lennarb/routes.rb', line 57

def freeze
  @frozen = true
  deep_freeze(@store)
  self
end

#frozen?Boolean

Check if the routes are frozen

Returns:

  • (Boolean)

    True if frozen



66
67
68
# File 'lib/lennarb/routes.rb', line 66

def frozen?
  @frozen
end

#match_route(parts, http_method) ⇒ Array(Proc, Hash)?

Match a route with the given path parts and HTTP method

Parameters:

  • parts (Array<String>)

    Path parts

  • http_method (Symbol)

    HTTP method

Returns:

  • (Array(Proc, Hash), nil)

    Route handler and params, or nil if no match



34
35
36
# File 'lib/lennarb/routes.rb', line 34

def match_route(parts, http_method)
  @store.match_route(parts, http_method)
end

#merge!(other) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copy the routes from another Routes instance into this one.

The copy is deep: node objects are rebuilt rather than shared, so routes registered on the source afterwards cannot leak into this instance.

Parameters:

  • other (Routes)

    The routes to copy from

Returns:

  • (self)


46
47
48
49
# File 'lib/lennarb/routes.rb', line 46

def merge!(other)
  @store.merge!(deep_copy(other.store))
  self
end

#root(&block) ⇒ void

This method returns an undefined value.

Define the root route (GET /)

Parameters:

  • block (Proc)

    Block to execute when route matches



25
26
27
# File 'lib/lennarb/routes.rb', line 25

def root(&block)
  get("/", &block)
end