Class: Reloj::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/reloj/core/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.



8
9
10
11
12
13
14
# File 'lib/reloj/core/router.rb', line 8

def initialize(pattern, http_method, controller_class, action_name)
  @pattern = pattern
  @http_method = http_method
  @controller_class = controller_class
  @action_name = action_name
  # save all helpers in the routes
end

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



6
7
8
# File 'lib/reloj/core/router.rb', line 6

def action_name
  @action_name
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



6
7
8
# File 'lib/reloj/core/router.rb', line 6

def controller_class
  @controller_class
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



6
7
8
# File 'lib/reloj/core/router.rb', line 6

def http_method
  @http_method
end

#patternObject (readonly)

Returns the value of attribute pattern.



6
7
8
# File 'lib/reloj/core/router.rb', line 6

def pattern
  @pattern
end

Instance Method Details

#matches?(req) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/reloj/core/router.rb', line 17

def matches?(req)
  req.request_method.downcase.to_sym == @http_method &&
  @pattern.match(req.path)
end

#run(req, res, paths = []) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/reloj/core/router.rb', line 23

def run(req, res, paths = [])
  route_params = {}
  matches = @pattern.match(req.path)
  matches.names.each do |key|
    route_params[key] = matches[key]
  end
  #pass the methods to the controller class here
  @controller_class.new(req, res, route_params, paths).invoke_action(@action_name)
end