Class: Rage::Router::HandlerStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/router/handler_storage.rb

Instance Method Summary collapse

Constructor Details

#initializeHandlerStorage

Returns a new instance of HandlerStorage.



4
5
6
7
8
9
# File 'lib/rage/router/handler_storage.rb', line 4

def initialize
  @unconstrained_handler = nil # optimized reference to the handler that will match most of the time
  @constraints = []
  @handlers = [] # unoptimized list of handler objects for which the fast matcher function will be compiled
  @constrained_handler_stores = nil
end

Instance Method Details

#add_handler(constrainer, route) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rage/router/handler_storage.rb', line 17

def add_handler(constrainer, route)
  params = route[:params]
  constraints = route[:constraints]

  handler_object = {
    params: params,
    constraints: constraints,
    handler: route[:handler],
    create_params_object: compile_create_params_object(params, route[:defaults], route[:meta])
  }

  constraints_keys = constraints.keys
  if constraints_keys.empty?
    @unconstrained_handler = handler_object
  end

  constraints_keys.each do |constraint_key|
    @constraints << constraint_key unless @constraints.include?(constraint_key)
  end

  if @handlers.length >= 32
    raise ArgumentError, "Limit reached: a maximum of 32 route handlers per node allowed when there are constraints"
  end

  @handlers << handler_object
  # Sort the most constrained handlers to the front of the list of handlers so they are tested first.
  @handlers.sort_by! { |a| a[:constraints].length }

  compile_get_handler_matching_constraints(constrainer)
end

#get_matching_handler(derived_constraints) ⇒ Object

This is the hot path for node handler finding -- change with care!



12
13
14
15
# File 'lib/rage/router/handler_storage.rb', line 12

def get_matching_handler(derived_constraints)
  return @unconstrained_handler unless derived_constraints
  get_handler_matching_constraints(derived_constraints)
end