class Spine::Routing::Route

Constants

EMPTY_WILDCARD_PATTERN
VARIABLES_PATTERN
WILDCARD_PATTERN

Attributes

app[R]
name[R]
pattern[R]
verb[R]

Public Class Methods

new(name, pattern, app, verb) click to toggle source
# File lib/spine/routing/route.rb, line 12
def initialize(name, pattern, app, verb)
  @name = name
  @pattern = pattern
  @app = app
  @verb = verb
end

Public Instance Methods

match(verb, path) click to toggle source
# File lib/spine/routing/route.rb, line 19
def match(verb, path)
  return nil unless verb == self.verb

  matches = path.match(regex)
  return nil unless matches

  Match.new(self, Hash[matches.names.zip(matches.captures)])
end

Private Instance Methods

add_format(path) click to toggle source
# File lib/spine/routing/route.rb, line 47
def add_format(path)
  path + '(?:\.(?<format>.*))?'
end
compile() click to toggle source
# File lib/spine/routing/route.rb, line 34
def compile
  Regexp.new("\\A#{ prepare }\/?\\Z")
end
prepare() click to toggle source
# File lib/spine/routing/route.rb, line 38
def prepare
  add_format(
    pattern
      .gsub(EMPTY_WILDCARD_PATTERN, '*paths\1')
      .gsub(WILDCARD_PATTERN,'(?<\1>[^.$]+)')
      .gsub(VARIABLES_PATTERN, '(?<\1>[^./$]+)')
  )
end
regex() click to toggle source
# File lib/spine/routing/route.rb, line 30
def regex
  @regex ||= compile
end