Class: RocketIO::Router

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

Constant Summary collapse

ROOT_PATH_PIECES =
[''.freeze].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*controllers) ⇒ Router

Returns a new instance of Router.



6
7
8
9
10
# File 'lib/rocketio/router.rb', line 6

def initialize *controllers
  @controllers = controllers.flatten.compact.uniq
  @routes = build_routes
  freeze!
end

Instance Attribute Details

#controllersObject (readonly)

Returns the value of attribute controllers.



4
5
6
# File 'lib/rocketio/router.rb', line 4

def controllers
  @controllers
end

#routesObject (readonly)

Returns the value of attribute routes.



4
5
6
# File 'lib/rocketio/router.rb', line 4

def routes
  @routes
end

Instance Method Details

#resolve_path(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rocketio/router.rb', line 12

def resolve_path path
  controllers = @routes
  pieces = path_pieces(path)
  depth  = -1
  controller  = nil
  offset = 0
  while controllers = controllers[pieces[depth += 1]]
    next unless controllers[0]
    controller  = controllers[0]
    offset = depth + 1
  end
  [controller, pieces[offset .. -1]]
end