Class: Route

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffold/lib/router/route.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.



4
5
6
# File 'lib/scaffold/lib/router/route.rb', line 4

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

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



2
3
4
# File 'lib/scaffold/lib/router/route.rb', line 2

def action_name
  @action_name
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



2
3
4
# File 'lib/scaffold/lib/router/route.rb', line 2

def controller_class
  @controller_class
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



2
3
4
# File 'lib/scaffold/lib/router/route.rb', line 2

def http_method
  @http_method
end

#patternObject (readonly)

Returns the value of attribute pattern.



2
3
4
# File 'lib/scaffold/lib/router/route.rb', line 2

def pattern
  @pattern
end

Instance Method Details

#matches?(req) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/scaffold/lib/router/route.rb', line 8

def matches?(req)
  method = req.params['_method'] || req.request_method
  pattern =~ req.path && http_method.to_s == method.downcase
end

#run(req, res, patterns) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/scaffold/lib/router/route.rb', line 13

def run(req, res, patterns)
  route_params = {}
  match_data = pattern.match(req.path)
  
  match_data.names.each do |key|
    route_params[key] = match_data[key]
  end
  
  controller = controller_class.new(req, res, route_params, patterns)
  controller.invoke_action(action_name)
end