Sha256: d27cece26795bb8f941b4b811c19bdffca18e5349f6030fb69af67c556d66f7a

Contents?: true

Size: 877 Bytes

Versions: 4

Compression:

Stored size: 877 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)
      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

    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

4 entries across 4 versions & 1 rubygems

Version Path
grape-route-helpers-2.1.0 lib/grape-route-helpers/named_route_matcher.rb
grape-route-helpers-2.0.0 lib/grape-route-helpers/named_route_matcher.rb
grape-route-helpers-1.2.2 lib/grape-route-helpers/named_route_matcher.rb
grape-route-helpers-1.2.1 lib/grape-route-helpers/named_route_matcher.rb