Sha256: eb360ab184ceea7de5c55c23e13033c2049a6610c0df1826911bfb1f5c6e8243

Contents?: true

Size: 827 Bytes

Versions: 3

Compression:

Stored size: 827 Bytes

Contents

module GrapeRouteHelpers
  # 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, *arguments)
      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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-route-helpers-1.2.0 lib/grape-route-helpers/named_route_matcher.rb
grape-route-helpers-1.0.1 lib/grape-route-helpers/named_route_matcher.rb
grape-route-helpers-1.0.0 lib/grape-route-helpers/named_route_matcher.rb