Module: GrapePathHelpers::NamedRouteMatcher
- Defined in:
- lib/grape-path-helpers/named_route_matcher.rb
Overview
methods to extend Grape::Endpoint so that calls to unknown methods will look for a route with a matching helper function name
Instance Method Summary collapse
- #grape_route_path?(method_name) ⇒ Boolean
-
#method_missing(method_name, *args) ⇒ Object
Dispatch method: short-circuits when the call can't be a route helper, otherwise finds the decorated route that matches the supplied segments.
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Dispatch method: short-circuits when the call can't be a route helper, otherwise finds the decorated route that matches the supplied segments. rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 11 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 |
Instance Method Details
#grape_route_path?(method_name) ⇒ Boolean
40 41 42 43 44 |
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 40 def grape_route_path?(method_name) method_name.end_with?('_path') && !Grape::API::Instance .decorated_routes_by_helper_name[method_name].nil? end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
36 37 38 |
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 36 def respond_to_missing?(method_name, _include_private = false) grape_route_path?(method_name) || super end |