Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor route public_send to AttributeTranslator #2050

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [#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).
* [#2039](https://github.com/ruby-grape/grape/pull/2039): Travis - update rails versions - [@ericproulx](https://github.com/ericproulx).
* [#2038](https://github.com/ruby-grape/grape/pull/2038): Travis - update ruby versions - [@ericproulx](https://github.com/ericproulx).
* [#2050](https://github.com/ruby-grape/grape/pull/2050): Refactor route public_send to AttributeTranslator - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
11 changes: 1 addition & 10 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ module Grape
class Router
attr_reader :map, :compiled

class Any < AttributeTranslator
attr_reader :pattern, :index
def initialize(pattern, index, **attributes)
@pattern = pattern
@index = index
super(attributes)
end
end

class NormalizePathCache < Grape::Util::Cache
def initialize
@cache = Hash.new do |h, path|
Expand Down Expand Up @@ -64,7 +55,7 @@ def append(route)

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

def call(env)
Expand Down
25 changes: 23 additions & 2 deletions lib/grape/router/attribute_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ class Router
class AttributeTranslator
attr_reader :attributes, :request_method, :requirements

ROUTE_ATTRIBUTES = %i[
prefix
version
settings
format
description
http_codes
headers
entity
details
requirements
request_method
namespace
].freeze

ROUTER_ATTRIBUTES = %i[pattern index].freeze

def initialize(attributes = {})
@attributes = attributes
@request_method = attributes[:request_method]
@requirements = attributes[:requirements]
end

(ROUTER_ATTRIBUTES + ROUTE_ATTRIBUTES).each do |attr|
define_method attr do
attributes[attr]
end
end

def to_h
Expand Down
20 changes: 1 addition & 19 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Route

extend Forwardable
def_delegators :pattern, :path, :origin
delegate Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES => :attributes

def method_missing(method_id, *arguments)
match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s)
Expand All @@ -34,25 +35,6 @@ def respond_to_missing?(method_id, _)
ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
end

%i[
prefix
version
settings
format
description
http_codes
headers
entity
details
requirements
request_method
namespace
].each do |method_name|
define_method method_name do
attributes.public_send method_name
end
end

def route_method
warn_route_methods(:method, caller(1).shift, :request_method)
request_method
Expand Down