class CottonTail::RouteSegment

RouteSegment implements the pattern matching for route segments

Constants

HASH
NAMED
NAMED_HASH
NAMED_STAR
STAR
TRANSFORM

Public Class Methods

new(value) click to toggle source
Calls superclass method
# File lib/cotton_tail/route_segment.rb, line 6
def initialize(value)
  @value = value
  super Regexp.new definition(value)
end

Public Instance Methods

binding() click to toggle source
# File lib/cotton_tail/route_segment.rb, line 19
def binding
  return '*' if star?

  return '#' if hash?

  @value
end
hash?() click to toggle source
# File lib/cotton_tail/route_segment.rb, line 15
def hash?
  /^#{HASH}|#{NAMED_HASH}$/.match? @value
end
star?() click to toggle source
# File lib/cotton_tail/route_segment.rb, line 11
def star?
  /^#{STAR}|#{NAMED_STAR}$/.match? @value
end

Private Instance Methods

definition(value) click to toggle source
# File lib/cotton_tail/route_segment.rb, line 31
def definition(value)
  transformers.reduce(value, &TRANSFORM)
end
sub_multi_wildcard(pattern) click to toggle source
# File lib/cotton_tail/route_segment.rb, line 51
def sub_multi_wildcard(pattern)
  pattern.gsub(HASH, '([^.]{0,}\.?)+')
end
sub_named_group_wildcard(pattern) click to toggle source

Converts named route segment to Regexp named capture group

"#:foo" -> "(?<foo>.+)"
# File lib/cotton_tail/route_segment.rb, line 37
def sub_named_group_wildcard(pattern)
  pattern.gsub(NAMED_HASH, '(?<\1>.+)')
end
sub_named_single_wildcard(pattern) click to toggle source

Converts named route segment to Regexp named capture group

"*:foo" -> "(?<foo>[^.]+)"
# File lib/cotton_tail/route_segment.rb, line 43
def sub_named_single_wildcard(pattern)
  pattern.gsub(NAMED_STAR, '(?<\1>[^.]+)')
end
sub_single_wildcard(pattern) click to toggle source
# File lib/cotton_tail/route_segment.rb, line 47
def sub_single_wildcard(pattern)
  pattern.gsub(STAR, '([^.]+)')
end
transformers() click to toggle source
# File lib/cotton_tail/route_segment.rb, line 55
def transformers
  [
    method(:sub_named_group_wildcard),
    method(:sub_named_single_wildcard),
    method(:sub_single_wildcard),
    method(:sub_multi_wildcard)
  ]
end