class Slimer::WebRoute

@abstract A simple Rack request extractor and matcher

Constants

NAMED_SEGMENTS_PATTERN

Attributes

block[RW]
name[RW]
pattern[RW]
request_method[RW]

Public Class Methods

new(request_method, pattern, block) click to toggle source
# File lib/slimer/web/router.rb, line 75
def initialize(request_method, pattern, block)
  @request_method = request_method
  @pattern = pattern
  @block = block
end

Public Instance Methods

compile() click to toggle source
# File lib/slimer/web/router.rb, line 85
def compile
  if pattern.match?(NAMED_SEGMENTS_PATTERN)
    p = pattern.gsub(NAMED_SEGMENTS_PATTERN, '/\1(?<\2>[^$/]+)')

    Regexp.new("\\A#{p}\\Z")
  else
    pattern
  end
end
match(_request_method, path) click to toggle source
# File lib/slimer/web/router.rb, line 95
def match(_request_method, path)
  case matcher
  when String
    {} if path == matcher
  else
    path_match = path.match(matcher)
    path_match&.named_captures&.transform_keys(&:to_sym)
  end
end
matcher() click to toggle source
# File lib/slimer/web/router.rb, line 81
def matcher
  @matcher ||= compile
end