module GrapeRouteHelpers::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_id, *arguments) click to toggle source
Calls superclass method
# File lib/grape-route-helpers/named_route_matcher.rb, line 6
def method_missing(method_id, *arguments)
  super unless method_id.to_s.match(/_path$/)
  segments = arguments.first || {}

  route = Grape::API.decorated_routes.detect do |r|
    route_match?(r, method_id, segments)
  end

  if route
    route.send(method_id, *arguments)
  else
    super
  end
end
route_match?(route, method_name, segments) click to toggle source
# File lib/grape-route-helpers/named_route_matcher.rb, line 21
def route_match?(route, method_name, segments)
  return false unless route.respond_to?(method_name)
  fail ArgumentError,
       'Helper options must be a hash' unless segments.is_a?(Hash)
  requested_segments = segments.keys.map(&:to_s)
  route.uses_segments_in_path_helper?(requested_segments)
end