module GrapePathHelpers::NamedRouteMatcher

methods to extend Grape::Endpoint so that calls to unknown methods will look for a route with a matching helper function name

Public Instance Methods

method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/grape-path-helpers/named_route_matcher.rb, line 6
def method_missing(method_name, *args)
  possible_routes = Grape::API::Instance
                    .decorated_routes_by_helper_name[method_name]
  return super unless possible_routes

  segments = args.first || {}
  return super unless segments.is_a?(Hash)

  requested_segments = segments.keys.map(&:to_s)

  route = possible_routes.detect do |r|
    r.uses_segments_in_path_helper?(requested_segments)
  end

  if route
    route.send(method_name, *args)
  else
    super
  end
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
Calls superclass method
# File lib/grape-path-helpers/named_route_matcher.rb, line 28
def respond_to_missing?(method_name, _include_private = false)
  !Grape::API::Instance.decorated_routes_by_helper_name[method_name].nil? ||
    super
end