Sha256: 669b5cd131c6a4ad2635596d9236b97c7d6fd755d074d5620c7401aec61486ed

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

module Restfulness

  class Route

    # The path array of elements, :id always on end!
    attr_accessor :path

    # Reference to the class that will handle requests for this route
    attr_accessor :resource_name

    def initialize(*args)
      self.resource_name = args.pop.to_s
      self.path = args.reject{|arg| arg == :id}

      if resource_name.empty? || resource.nil? # Try to load the resource
        raise ArgumentError, "Please provide a resource!"
      end
    end

    def build_path(path)
      Path.new(self, path)
    end

    def handles?(parts)
      # Make sure same length (accounting for id)
      diff = parts.length - path.length
      return false if diff != 0 && diff != 1

      # Compare the pairs
      path.each_with_index do |slug, i|
        if slug.is_a?(String) or slug.is_a?(Numeric)
          return false if parts[i] != slug.to_s
        end
      end
      true
    end

    def resource
      resource_name.constantize
    end

    def build_resource(request, response)
      resource.new(request, response)
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
restfulness-0.3.6 lib/restfulness/route.rb
restfulness-0.3.5 lib/restfulness/route.rb
restfulness-0.3.4 lib/restfulness/route.rb
restfulness-0.3.3 lib/restfulness/route.rb
restfulness-0.3.2 lib/restfulness/route.rb
restfulness-0.3.1 lib/restfulness/route.rb
restfulness-0.3.0 lib/restfulness/route.rb
restfulness-0.2.6 lib/restfulness/route.rb
restfulness-0.2.5 lib/restfulness/route.rb
restfulness-0.2.4 lib/restfulness/route.rb
restfulness-0.2.3 lib/restfulness/route.rb