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
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 6
def method_missing(method_id, *arguments)
return super unless method_id.to_s =~ /_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
|
Instance Method Details
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
22
23
24
25
26
27
28
29
30
|
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 22
def respond_to_missing?(method_name, _include_private = false)
return super unless method_name =~ /_path$/
Grape::API.decorated_routes.detect do |route|
return true if route.respond_to?(method_name)
end
super
end
|
#route_match?(route, method_name, segments) ⇒ Boolean
32
33
34
35
36
37
38
39
|
# File 'lib/grape-path-helpers/named_route_matcher.rb', line 32
def route_match?(route, method_name, segments)
return false unless route.respond_to?(method_name)
raise 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
|