class Orbit::Routing::Path

Attributes

ignore[RW]
keys[RW]
name[R]
regex[R]

Public Class Methods

new(path) click to toggle source
# File lib/orbit/routing/path.rb, line 6
def initialize(path)
  @name = path
  @regex, @keys = compile
end

Public Instance Methods

compile() click to toggle source
# File lib/orbit/routing/path.rb, line 22
def compile
  return compile_string if name.respond_to? :to_str
    
  if is_hash?
    [name, name.keys]
  elsif is_object?
    [name, name.names]
  elsif name.respond_to? :match
    [name, []]
  else
    raise TypeError, name
  end
end
get_params(path) click to toggle source
# File lib/orbit/routing/path.rb, line 11
def get_params(path)
  matches = regex.match(path)
  {}.tap do |params|
    keys.each_with_index do |key, index|
      match_index = index + 1

      params[key.to_sym] = (matches[match_index]) if matches && matches.size > match_index
    end
  end
end

Private Instance Methods

compile_string() click to toggle source
# File lib/orbit/routing/path.rb, line 47
def compile_string
  # Special case handling.
  #
  last_segment = segments.last

  if last_segment.match(/\[\^\\\./)
    parts = last_segment.rpartition(/\[\^\\\./)
    parts[1] = '[^'
    segments[-1] = parts.join
  end

  [/\A#{segments.join('/')}\z/, keys]
end
is_hash?() click to toggle source
# File lib/orbit/routing/path.rb, line 39
def is_hash?
  name.respond_to?(:keys) && name.respond_to?(:match)
end
is_object?() click to toggle source
# File lib/orbit/routing/path.rb, line 43
def is_object?
  name.respond_to?(:names) && name.respond_to?(:match)
end
segments() click to toggle source

Split the path into pieces in between forward slashes. A negative number is given as the second argument of path.split because with this number, the method does not ignore / at the end and appends an empty string at the end of the return value.

# File lib/orbit/routing/path.rb, line 66
def segments
  @segments = begin
    @keys = []
    @ignore = []

    name.split('/', -1).map! do |segment|
      pattern_keys, pattern_ignore, result = Pattern.resolve(segment)
      
      @keys += pattern_keys
      @ignore += pattern_ignore

      result
    end
  end
end