Sha256: 87159b075d15dfc4c522a55f13ba803f99b2035e5471d2c3ed3f751d5304d2b9

Contents?: true

Size: 879 Bytes

Versions: 15

Compression:

Stored size: 879 Bytes

Contents

module Restfulness

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

    attr_accessor :route, :components, :params

    def initialize(route, string)
      self.route  = route
      self.params = {}
      parse(string)
    end

    def to_s
      '/' + components.join('/')
    end

    def [](index)
      if index.is_a?(Integer)
        components[index]
      else
        params[index]
      end
    end

    protected

    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

  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
restfulness-0.3.6 lib/restfulness/path.rb
restfulness-0.3.5 lib/restfulness/path.rb
restfulness-0.3.4 lib/restfulness/path.rb
restfulness-0.3.3 lib/restfulness/path.rb
restfulness-0.3.2 lib/restfulness/path.rb
restfulness-0.3.1 lib/restfulness/path.rb
restfulness-0.3.0 lib/restfulness/path.rb
restfulness-0.2.6 lib/restfulness/path.rb
restfulness-0.2.5 lib/restfulness/path.rb
restfulness-0.2.4 lib/restfulness/path.rb
restfulness-0.2.3 lib/restfulness/path.rb
restfulness-0.2.2 lib/restfulness/path.rb
restfulness-0.2.1 lib/restfulness/path.rb
restfulness-0.2.0 lib/restfulness/path.rb
restfulness-0.1.0 lib/restfulness/path.rb