class Sr::Jimson::Router::Map
Provides a DSL for routing method namespaces to handlers. Only handles root-level and non-nested namespaces, e.g. 'foo.bar' or 'foo'.
Public Class Methods
new()
click to toggle source
# File lib/sr/jimson/router/map.rb, line 10 def initialize @routes = {} end
Public Instance Methods
handler_for_method(method)
click to toggle source
Return the handler for a (possibly namespaced) method name
# File lib/sr/jimson/router/map.rb, line 40 def handler_for_method(method) parts = method.split('.') ns = (method.index('.') == nil ? '' : parts.first) handler = @routes[ns] if handler.is_a?(Sr::Jimson::Router::Map) return handler.handler_for_method(parts[1..-1].join('.')) end handler end
jimson_methods()
click to toggle source
Return an array of all methods on handlers in the map, fully namespaced
# File lib/sr/jimson/router/map.rb, line 60 def jimson_methods arr = @routes.keys.map do |ns| prefix = (ns == '' ? '' : "#{ns}.") handler = @routes[ns] if handler.is_a?(Sr::Jimson::Router::Map) handler.jimson_methods else handler.class.jimson_exposed_methods.map { |method| prefix + method } end end arr.flatten end
namespace(ns, handler = nil, &block)
click to toggle source
Define the handler for a namespace
# File lib/sr/jimson/router/map.rb, line 25 def namespace(ns, handler = nil, &block) if !!handler handler = handler.new if handler.is_a?(Class) @routes[ns.to_s] = handler else # passed a block for nested namespacing map = Sr::Jimson::Router::Map.new @routes[ns.to_s] = map map.instance_eval(&block) end end
root(handler)
click to toggle source
Set the root handler, i.e. the handler used for a bare method like 'foo'
# File lib/sr/jimson/router/map.rb, line 17 def root(handler) handler = handler.new if handler.is_a?(Class) @routes[''] = handler end
strip_method_namespace(method)
click to toggle source
Strip off the namespace part of a method and return the bare method name
# File lib/sr/jimson/router/map.rb, line 53 def strip_method_namespace(method) method.split('.').last end