Class: Restfulness::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulness/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Route

Returns a new instance of Route.



11
12
13
14
15
16
17
18
# File 'lib/restfulness/route.rb', line 11

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

Instance Attribute Details

#pathObject

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



6
7
8
# File 'lib/restfulness/route.rb', line 6

def path
  @path
end

#resource_nameObject

Reference to the class that will handle requests for this route



9
10
11
# File 'lib/restfulness/route.rb', line 9

def resource_name
  @resource_name
end

Instance Method Details

#build_path(path) ⇒ Object



20
21
22
# File 'lib/restfulness/route.rb', line 20

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

#build_resource(request, response) ⇒ Object



42
43
44
# File 'lib/restfulness/route.rb', line 42

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

#handles?(parts) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/restfulness/route.rb', line 24

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

#resourceObject



38
39
40
# File 'lib/restfulness/route.rb', line 38

def resource
  resource_name.constantize
end