lib/lotus/routing/endpoint_resolver.rb in lotus-router-0.1.0 vs lib/lotus/routing/endpoint_resolver.rb in lotus-router-0.1.1
- old
+ new
@@ -24,11 +24,11 @@
# router = Lotus::Router.new do
# get '/', to: 'articles#show'
# end
#
# # That string is transformed into "Articles(::Controller::|Controller::)Show"
- # # because the risolver is able to lookup (in the given order) for:
+ # # because the resolver is able to lookup (in the given order) for:
# #
# # * Articles::Controller::Show
# # * ArticlesController::Show
# #
# # When it finds a class, it stops the lookup and returns the result.
@@ -63,10 +63,15 @@
# lookup for controllers and actions. (defaults to `Object`)
#
# @option options [String] :suffix the suffix appended to the controller
# name during the lookup. (defaults to `SUFFIX`)
#
+ # @option options [String] :pattern the string to interpolate in order
+ # to return an action name. Please note that this option override
+ # :suffix. This string SHOULD contain `'%{controller}'` and `'%{action}'`,
+ # all the other keys will be ignored. See the examples below.
+ #
# @option options [String] :action_separator the sepatator between controller and
# action name. (defaults to `ACTION_SEPARATOR`)
#
#
#
@@ -112,10 +117,22 @@
# # * ArticlesController::Show
# # * ArticlesCtrl::Show
#
#
#
+ # @example Specify custom simple pattern
+ # require 'lotus/router'
+ #
+ # resolver = Lotus::Routing::EndpointResolver.new(pattern: 'Controllers::%{controller}::%{action}')
+ # router = Lotus::Router.new(resolver: resolver)
+ #
+ # router.get('/', to: 'articles#show')
+ # # => Will look for:
+ # # * Controllers::Articles::Show
+ #
+ #
+ #
# @example Specify custom controller-action separator
# require 'lotus/router'
#
# resolver = Lotus::Routing::EndpointResolver.new(separator: '@')
# router = Lotus::Router.new(resolver: resolver)
@@ -125,12 +142,13 @@
# # * Articles::Controller::Show
# # * ArticlesController::Show
def initialize(options = {})
@endpoint_class = options[:endpoint] || Endpoint
@namespace = options[:namespace] || Object
- @suffix = options[:suffix] || SUFFIX
@action_separator = options[:action_separator] || ACTION_SEPARATOR
+ @pattern = options[:pattern] ||
+ "%{controller}#{options[:suffix] || SUFFIX}%{action}"
end
# Resolve the given set of HTTP verb, path, endpoint and options.
# If it fails to resolve, it will mount the default endpoint to the given
# path, which returns an 404 (Not Found).
@@ -227,10 +245,12 @@
private
def resolve_callable(callable)
if callable.respond_to?(:call)
@endpoint_class.new(callable)
+ elsif callable.is_a?(Class) && callable.instance_methods.include?(:call)
+ @endpoint_class.new(callable.new)
end
end
def resolve_matchable(matchable)
if matchable.respond_to?(:match)
@@ -241,10 +261,10 @@
end
def resolve_action(string)
if string.match(action_separator)
controller, action = string.split(action_separator).map {|token| classify(token) }
- controller + @suffix + action
+ @pattern % {controller: controller, action: action}
end
end
end
end
end