Class: Wurk::Web::Extension::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/web/extension.rb

Overview

Compiles a Sinatra-style path (“/locks/:digest”) into a matcher that extracts Symbol-keyed route params. A trailing “*” matches the rest.

Constant Summary collapse

NAMED =
%r{/([^/]*):([^./:$]+)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Route

Returns a new instance of Route.



63
64
65
66
67
# File 'lib/wurk/web/extension.rb', line 63

def initialize(path)
  @path = path.to_s
  @keys = []
  @regex = compile(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



61
62
63
# File 'lib/wurk/web/extension.rb', line 61

def path
  @path
end

Instance Method Details

#match(path) ⇒ Object

Returns a ‘=> value` Hash of route params, or nil if no match.



70
71
72
73
74
75
# File 'lib/wurk/web/extension.rb', line 70

def match(path)
  m = @regex.match(path)
  return nil unless m

  @keys.each_with_index.to_h { |k, i| [k, m[i + 1] && CGI.unescape(m[i + 1])] }
end