Sha256: 9435679b8e33a0fa66151ab3088889d732c6886087458023a4b8cfff9362e4d7

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

module GrapePathHelpers
  # methods to extend Grape::Endpoint so that calls
  # to unknown methods will look for a route with a matching
  # helper function name
  module NamedRouteMatcher
    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
    ruby2_keywords(:method_missing)

    def respond_to_missing?(method_name, _include_private = false)
      !Grape::API::Instance.decorated_routes_by_helper_name[method_name].nil? ||
        super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-path-helpers-1.7.0 lib/grape-path-helpers/named_route_matcher.rb