Class: Puffs::Route

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

Overview

Router is comprised of Routes.

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.



53
54
55
56
57
58
# File 'lib/router.rb', line 53

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.



51
52
53
# File 'lib/router.rb', line 51

def action_name
  @action_name
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



51
52
53
# File 'lib/router.rb', line 51

def controller_class
  @controller_class
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



51
52
53
# File 'lib/router.rb', line 51

def http_method
  @http_method
end

#patternObject (readonly)

Returns the value of attribute pattern.



51
52
53
# File 'lib/router.rb', line 51

def pattern
  @pattern
end

Instance Method Details

#matches?(req) ⇒ Boolean

checks if pattern matches path and method matches request method

Returns:

  • (Boolean)


61
62
63
# File 'lib/router.rb', line 61

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

#run(req, res) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/router.rb', line 67

def run(req, res)
  matches = pattern.match(req.path)
  route_params = {}

  matches.names.each do |name|
    route_params[name] = matches[name]
  end

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