Class: Clutterbuck::Router::Route

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

Overview

A route within the application.

Not something you should ever have to deal with yourself directly, it's part of the internal plumbing of Clutterbuck::Router.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, path_match, method) ⇒ Route

Returns a new instance of Route.

Parameters:

  • verb (String)
  • path_match (String, Regexp)
  • method (UnboundMethod)

    is a method to call on an instance of the app class this route is defined on, which will do whatever is needed to handle the route. Because you can't just create an UnboundMethod out of thin air, the UnboundMethod needs to be created in the class, and then passed into here. Ugly.



24
25
26
27
28
29
30
31
# File 'lib/clutterbuck/router/route.rb', line 24

def initialize(verb, path_match, method)
  unless path_match.is_a?(String) or path_match.is_a?(Regexp)
    raise ArgumentError,
          "path must be either a string or a regexp"
  end

  @verb, @path_match, @method = verb, path_match, method
end

Instance Attribute Details

#path_matchObject

Returns the value of attribute path_match.



12
13
14
# File 'lib/clutterbuck/router/route.rb', line 12

def path_match
  @path_match
end

#verbObject

Returns the value of attribute verb.



12
13
14
# File 'lib/clutterbuck/router/route.rb', line 12

def verb
  @verb
end

Instance Method Details

#handles?(path) ⇒ Boolean

Can this route handle a request for the specified path?

Parameters:

  • path (String)

Returns:

  • (Boolean)

    Boolean



39
40
41
42
43
44
45
46
# File 'lib/clutterbuck/router/route.rb', line 39

def handles?(path)
  !!case @path_match
  when String
    @path_match == path
  when Regexp
    @path_match =~ path
  end
end

#run(obj, path) ⇒ Object

Execute the handler for the route

Run the method



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

def run(obj, path)
  args = case @path_match
    when String then []
    when Regexp then @path_match.match(path)[1..-1]
  end

  @method.bind(obj).call(*args)
end