Sha256: b28729666909dd16266b9a00efa83079041ed32cbe81c370c830743e4a31bc0b

Contents?: true

Size: 1.12 KB

Versions: 1

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_id, *args)
      return super unless method_id.to_s =~ /_path$/

      segments = args.first || {}

      return super unless segments.is_a?(Hash)

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

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

      if route
        route.send(method_id, *args)
      else
        super
      end
    end
    ruby2_keywords(:method_missing)

    def respond_to_missing?(method_name, _include_private = false)
      return super unless method_name =~ /_path$/

      Grape::API::Instance.decorated_routes.detect do |route|
        return true if route.respond_to?(method_name)
      end

      super
    end

    def route_match?(route, method_name, requested_segments)
      return false unless route.respond_to?(method_name)

      route.uses_segments_in_path_helper?(requested_segments)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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