Class: Maveric::Router

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

Overview

Storage of routes and routing mechanisms.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Router

Returns a new instance of Router.



774
775
776
# File 'lib/maveric.rb', line 774

def initialize opts={}
  @routings = Hash.new
end

Instance Method Details

#[](path) ⇒ Object

Matches the given path against the collection of routes. Returns nil the appropriate Route#route result with :controller mapped to the associated Controller.



808
809
810
811
812
813
# File 'lib/maveric.rb', line 808

def [] path
  ::Maveric.type_check :path, path, String
  @routings.eject do |(r,c)|
    b=r.route(path) and {:controller=>c}.update b
  end
end

#add(routes, controller, opts = {}) ⇒ Object

Places one or more routes to a Controller. Will generate a new Route if a route is a String. Accepts an Array for routes to add several at a time.



786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/maveric.rb', line 786

def add routes, controller, opts={}
  ::Maveric.log.info "#{self.class}#add"+
    " #{routes.inspect} #{controller.inspect}"
  ::Maveric.type_check :controller, controller, Class
  ::Maveric.type_check :routes, routes, Array, String, ::Maveric::Route
  routes = [routes] unless routes.is_a? Array
  routes.flatten.map do |route|
    ::Maveric.type_check :route, route, String, ::Maveric::Route
    route = ::Maveric::Route.new route, opts if route.instance_of? String
    @routings[route] = controller
  end
end

#controllersObject

Returns a list of the controllers with listed routes.



779
780
781
# File 'lib/maveric.rb', line 779

def controllers
  @routings.values.uniq
end

#to(controller) ⇒ Object

Return an array of routes mapped to a controller.



800
801
802
# File 'lib/maveric.rb', line 800

def to controller
  @routings.map{|(r,d)| r if d == controller }.compact
end