Skip to content

Commit 2a4e2c2

Browse files
authored
Merge pull request #2050 from ericproulx/attribute_translator_define_method
Refactor route public_send to AttributeTranslator
2 parents 01ca08c + 67f9417 commit 2a4e2c2

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#2048](https://github.com/ruby-grape/grape/issues/2034): Grape Enterprise support is now available [via TideLift](https://tidelift.com/subscription/request-a-demo?utm_source=rubygems-grape&utm_medium=referral&utm_campaign=enterprise) - [@dblock](https://github.com/dblock).
66
* [#2039](https://github.com/ruby-grape/grape/pull/2039): Travis - update rails versions - [@ericproulx](https://github.com/ericproulx).
77
* [#2038](https://github.com/ruby-grape/grape/pull/2038): Travis - update ruby versions - [@ericproulx](https://github.com/ericproulx).
8+
* [#2050](https://github.com/ruby-grape/grape/pull/2050): Refactor route public_send to AttributeTranslator - [@ericproulx](https://github.com/ericproulx).
89
* Your contribution here.
910

1011
#### Fixes

lib/grape/router.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ module Grape
77
class Router
88
attr_reader :map, :compiled
99

10-
class Any < AttributeTranslator
11-
attr_reader :pattern, :index
12-
def initialize(pattern, index, **attributes)
13-
@pattern = pattern
14-
@index = index
15-
super(attributes)
16-
end
17-
end
18-
1910
class NormalizePathCache < Grape::Util::Cache
2011
def initialize
2112
@cache = Hash.new do |h, path|
@@ -64,7 +55,7 @@ def append(route)
6455

6556
def associate_routes(pattern, **options)
6657
@neutral_regexes << Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
67-
@neutral_map << Any.new(pattern, @neutral_map.length, **options)
58+
@neutral_map << Grape::Router::AttributeTranslator.new(options.merge(pattern: pattern, index: @neutral_map.length))
6859
end
6960

7061
def call(env)

lib/grape/router/attribute_translator.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,31 @@ class Router
66
class AttributeTranslator
77
attr_reader :attributes, :request_method, :requirements
88

9+
ROUTE_ATTRIBUTES = %i[
10+
prefix
11+
version
12+
settings
13+
format
14+
description
15+
http_codes
16+
headers
17+
entity
18+
details
19+
requirements
20+
request_method
21+
namespace
22+
].freeze
23+
24+
ROUTER_ATTRIBUTES = %i[pattern index].freeze
25+
926
def initialize(attributes = {})
1027
@attributes = attributes
11-
@request_method = attributes[:request_method]
12-
@requirements = attributes[:requirements]
28+
end
29+
30+
(ROUTER_ATTRIBUTES + ROUTE_ATTRIBUTES).each do |attr|
31+
define_method attr do
32+
attributes[attr]
33+
end
1334
end
1435

1536
def to_h

lib/grape/router/route.rb

+1-19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Route
1818

1919
extend Forwardable
2020
def_delegators :pattern, :path, :origin
21+
delegate Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES => :attributes
2122

2223
def method_missing(method_id, *arguments)
2324
match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s)
@@ -34,25 +35,6 @@ def respond_to_missing?(method_id, _)
3435
ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
3536
end
3637

37-
%i[
38-
prefix
39-
version
40-
settings
41-
format
42-
description
43-
http_codes
44-
headers
45-
entity
46-
details
47-
requirements
48-
request_method
49-
namespace
50-
].each do |method_name|
51-
define_method method_name do
52-
attributes.public_send method_name
53-
end
54-
end
55-
5638
def route_method
5739
warn_route_methods(:method, caller(1).shift, :request_method)
5840
request_method

0 commit comments

Comments
 (0)