Module: Lego::Controller::RouteHandler

Extended by:
RouteHandler
Included in:
RouteHandler
Defined in:
lib/lego/controller/route_handler.rb

Overview

RouteHandler is the module that holds and handles routes for every controller class.

Defined Under Namespace

Classes: InvalidMatcher

Instance Method Summary collapse

Instance Method Details

#add_matcher(module_name) ⇒ Object

Raises:



29
30
31
32
# File 'lib/lego/controller/route_handler.rb', line 29

def add_matcher(module_name)
  raise InvalidMatcher if not validate_matcher(module_name)
  matchers << module_name
end

#add_route(method, options) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lego/controller/route_handler.rb', line 21

def add_route(method, options)
  if method == :not_found
    routes[:not_found] = options
  else
    routes[method] << options 
  end
end

#extended(base) ⇒ Object

When extended we need to copy out matchers and routes to the class extending.



17
18
19
# File 'lib/lego/controller/route_handler.rb', line 17

def extended(base)
  base.matchers.concat(matchers)
end

#match_all_routes(env) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/lego/controller/route_handler.rb', line 58

def match_all_routes(env)
  method = extract_method_from_env(env)
  routes[method].each do |route|
    if match_data = run_matchers(route, env)
      return [route] | match_data
    end
  end
  nil 
end

#matchersObject

Getter for instance variable holding matchers.



42
43
44
# File 'lib/lego/controller/route_handler.rb', line 42

def matchers
  cached_matchers
end

#routesObject

Getter for cached instance variable holding routes.



36
37
38
# File 'lib/lego/controller/route_handler.rb', line 36

def routes
  cached_routes
end

#run_matchers(route, env) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/lego/controller/route_handler.rb', line 50

def run_matchers(route, env)
  matchers.each do |matcher|
    match = matcher.match_route(route, env)
    return match if match.kind_of?(Array)
  end
  false
end

#validate_matcher(module_name) ⇒ Object



46
47
48
# File 'lib/lego/controller/route_handler.rb', line 46

def validate_matcher(module_name)
  eval(module_name.to_s).respond_to?(:match_route)
end