Skip to content

Commit

Permalink
method missing to_ary (ruby-grape#2370)
Browse files Browse the repository at this point in the history
* Add to_ary nil

* Add changelog

* Remove method_missing and warning

* Update CHANGELOG.md

* Add upgrading notes
Replace route_params by params

* Update README.md
  • Loading branch information
ericproulx authored Nov 20, 2023
1 parent 76cae59 commit 3f01d03
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#### Fixes

* [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
* [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia).
* Your contribution here.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3352,7 +3352,7 @@ You can access the controller params, headers, and helpers through the context w

Grape routes can be reflected at runtime. This can notably be useful for generating documentation.

Grape exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`. You can add custom route settings to the route metadata with `route_setting`.
Grape exposes arrays of API versions and compiled routes. Each route contains a `prefix`, `version`, `namespace`, `method` and `params`. You can add custom route settings to the route metadata with `route_setting`.

```ruby
class TwitterAPI < Grape::API
Expand All @@ -3375,7 +3375,7 @@ TwitterAPI::routes[0].description # => 'Includes custom settings.'
TwitterAPI::routes[0].settings[:custom] # => { key: 'value' }
```

Note that `Route#route_xyz` methods have been deprecated since 0.15.0.
Note that `Route#route_xyz` methods have been deprecated since 0.15.0 and removed since 2.0.1.

Please use `Route#xyz` instead.

Expand All @@ -3395,7 +3395,7 @@ class MyAPI < Grape::API
requires :id, type: Integer, desc: 'Identity.'
end
get 'params/:id' do
route.route_params[params[:id]] # yields the parameter description
route.params[params[:id]] # yields the parameter description
end
end
```
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Upgrading Grape
===============

### Upgrading to >= 2.0.1

#### Grape::Router::Route.route_xxx methods have been removed

- `route_method` is accessible through `request_method`
- `route_path` is accessible through `path`
- Any other `route_xyz` are accessible through `options[xyz]`

### Upgrading to >= 2.0.0

#### Headers
Expand Down
37 changes: 0 additions & 37 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
require 'grape/router/pattern'
require 'grape/router/attribute_translator'
require 'forwardable'
require 'pathname'

module Grape
class Router
class Route
ROUTE_ATTRIBUTE_REGEXP = /route_([_a-zA-Z]\w*)/.freeze
SOURCE_LOCATION_REGEXP = /^(.*?):(\d+?)(?::in `.+?')?$/.freeze
FIXED_NAMED_CAPTURES = %w[format version].freeze

attr_accessor :pattern, :translator, :app, :index, :options
Expand All @@ -20,31 +17,6 @@ class Route
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)
if match
method_name = match.captures.last.to_sym
warn_route_methods(method_name, caller(1).shift)
@options[method_name]
else
super
end
end

def respond_to_missing?(method_id, _)
ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
end

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

def route_path
warn_route_methods(:path, caller(1).shift)
pattern.path
end

def initialize(method, pattern, **options)
method_s = method.to_s
method_upcase = Grape::Http::Headers.find_supported_method(method_s) || method_s.upcase
Expand Down Expand Up @@ -77,15 +49,6 @@ def params(input = nil)
parsed ? parsed.delete_if { |_, value| value.nil? }.symbolize_keys : {}
end
end

private

def warn_route_methods(name, location, expected = nil)
path, line = *location.scan(SOURCE_LOCATION_REGEXP).first
path = File.realpath(path) if Pathname.new(path).relative?
expected ||= name
Grape.deprecator.warn("#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.")
end
end
end
end
3 changes: 1 addition & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3050,7 +3050,6 @@ def static
expect(subject.routes.length).to eq(1)
route = subject.routes.first
expect(route.description).to eq('first method')
expect(route.route_foo).to be_nil
expect(route.params).to eq({})
expect(route.options).to be_a(Hash)
end
Expand Down Expand Up @@ -3095,7 +3094,7 @@ def static
get 'second'
end
expect(subject.routes.map do |route|
{ description: route.description, foo: route.route_foo, params: route.params }
{ description: route.description, foo: route.options[:foo], params: route.params }
end).to eq [
{ description: 'ns second', foo: 'bar', params: {} }
]
Expand Down

0 comments on commit 3f01d03

Please sign in to comment.