Class: TGauge::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, http_method, controller_class, action_name) ⇒ Route

Returns a new instance of Route.



5
6
7
8
9
10
# File 'lib/db/router.rb', line 5

def initialize(pattern, http_method, controller_class, action_name)
  @pattern = pattern
  @http_method = http_method
  @controller_class = controller_class
  @action_name = action_name
end

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



3
4
5
# File 'lib/db/router.rb', line 3

def action_name
  @action_name
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



3
4
5
# File 'lib/db/router.rb', line 3

def controller_class
  @controller_class
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



3
4
5
# File 'lib/db/router.rb', line 3

def http_method
  @http_method
end

#patternObject (readonly)

Returns the value of attribute pattern.



3
4
5
# File 'lib/db/router.rb', line 3

def pattern
  @pattern
end

Instance Method Details

#matches?(req) ⇒ Boolean

checks if pattern matches path and method matches request method

Returns:

  • (Boolean)


13
14
15
# File 'lib/db/router.rb', line 13

def matches?(req)
  (@pattern =~ req.path) == 0 && req.request_method.downcase == @http_method.to_s
end

#run(req, res) ⇒ Object

use pattern to pull out route params (save for later?) instantiate controller and call controller action



19
20
21
22
23
24
25
26
27
28
# File 'lib/db/router.rb', line 19

def run(req, res)
  match_data = @pattern.match(req.path)
  route_params = {}
  match_data.names.each do |key|
    route_params[key] = match_data[key]
  end

  controller = controller_class.new(req, res, route_params)
  controller.invoke_action(@action_name)
end