Class: Newark::Route

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

Defined Under Namespace

Classes: Constraint

Constant Summary collapse

PARAM_MATCHER =
/:(?<param>[^\/]*)/.freeze
PARAM_SUB =
/:[^\/]*/.freeze
PATH_MATCHER =
/\*(?<path>.*)/.freeze
PATH_SUB =
/\*.*/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, constraints, handler) ⇒ Route

Returns a new instance of Route.



11
12
13
14
15
16
17
# File 'lib/newark/route.rb', line 11

def initialize(path, constraints, handler)
  fail ArgumentError, 'You must define a route handler' if handler.nil?

  @constraints = Constraint.load(constraints)
  @handler     = handler
  @path        = path_matcher(path)
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



9
10
11
# File 'lib/newark/route.rb', line 9

def handler
  @handler
end

Instance Method Details

#match?(request) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/newark/route.rb', line 19

def match?(request)
  path_data = path_match?(request)
  (path_data && constraints_match?(request)).tap do |matched|
    if matched
      request.params.merge! Hash[ path_data.names.zip( path_data.captures ) ]
    end
  end
end