class 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(opts = {})
click to toggle source
# File lib/jimson/router/map.rb, line 10 def initialize(opts = {}) @routes = {} @opts = opts @ns_sep = opts[:ns_sep] || '.' end
Public Instance Methods
handler_for_method(method)
click to toggle source
Return the handler for a (possibly namespaced) method name
# File lib/jimson/router/map.rb, line 42 def handler_for_method(method) parts = method.split(@ns_sep) ns = (method.index(@ns_sep) == nil ? '' : parts.first) handler = @routes[ns] if handler.is_a?(Jimson::Router::Map) return handler.handler_for_method(parts[1..-1].join(@ns_sep)) end handler end
jimson_methods()
click to toggle source
Return an array of all methods on handlers in the map, fully namespaced
# File lib/jimson/router/map.rb, line 62 def jimson_methods arr = @routes.keys.map do |ns| prefix = (ns == '' ? '' : "#{ns}#{@ns_sep}") handler = @routes[ns] if handler.is_a?(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/jimson/router/map.rb, line 27 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 = Jimson::Router::Map.new(@opts) @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/jimson/router/map.rb, line 19 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/jimson/router/map.rb, line 55 def strip_method_namespace(method) method.split(@ns_sep).last end