class Restfulness::Path

The Path object is provided in request objects to provide easy access to parameters included in the URI's path.

Attributes

components[RW]
params[RW]
route[RW]

Public Class Methods

new(route, string) click to toggle source
# File lib/restfulness/path.rb, line 10
def initialize(route, string)
  self.route  = route
  self.params = {}
  parse(string)
end

Public Instance Methods

[](index) click to toggle source
# File lib/restfulness/path.rb, line 20
def [](index)
  if index.is_a?(Integer)
    components[index]
  else
    params[index]
  end
end
to_s() click to toggle source
# File lib/restfulness/path.rb, line 16
def to_s
  '/' + components.join('/')
end

Protected Instance Methods

parse(string) click to toggle source
# File lib/restfulness/path.rb, line 30
def parse(string)
  self.components = string.gsub(/^\/|\/$/, '').split(/\//)

  # Make sure we have the id available when parsing
  path = route.path + [:id]

  # Parametize values that need it
  path.each_with_index do |value, i|
    if value.is_a?(Symbol)
      params[value] = components[i]
    end
  end
end