Sha256: 9119bf71f45706bd08f8c5138721b8c7bc10442777a82a06fc2822343188fcb4

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

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)
      return super unless method_name.end_with?('_path')

      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_route_path?(method_name) || super
    end

    def grape_route_path?(method_name)
      method_name.end_with?('_path') &&
        !Grape::API::Instance
          .decorated_routes_by_helper_name[method_name].nil?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-path-helpers-2.0.1 lib/grape-path-helpers/named_route_matcher.rb
grape-path-helpers-2.0.0 lib/grape-path-helpers/named_route_matcher.rb
grape-path-helpers-1.7.1 lib/grape-path-helpers/named_route_matcher.rb