Class: Restfulness::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



8
9
10
11
12
# File 'lib/restfulness/router.rb', line 8

def initialize(&block)
  self.routes = []
  self.current_scope = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#current_scopeObject

Returns the value of attribute current_scope.



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

def current_scope
  @current_scope
end

#routesObject

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#add(*args, &block) ⇒ Object



14
15
16
17
18
# File 'lib/restfulness/router.rb', line 14

def add(*args, &block)
  # Do we need to pretend we've been given a scope?
  scope(*args[0..-2], &block) if block_given?
  routes << Route.new(*(current_scope + args))
end

#route_for(path) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/restfulness/router.rb', line 27

def route_for(path)
  parts = path.gsub(/^\/|\/$/, '').split(/\//)
  routes.each do |route|
    return route if route.handles?(parts)
  end
  nil
end

#scope(*args, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/restfulness/router.rb', line 20

def scope(*args, &block)
  old_scope = current_scope
  self.current_scope += args
  instance_eval(&block) if block_given?
  self.current_scope = old_scope
end