Skip to content

Commit 53d826f

Browse files
authored
Address some Ruby warnings (#1995)
* Address some Ruby warnings * Add warnings enablement to spec_helper.rb to see future warnings * Use instance_variable_defined? to clean up not initialized warnings * Add 'inherited' to NON_OVERRIDABLE list * Add multi_xml to test dependencies in Gemfile to address spec failure * Add changelog entry * expand changelog entry * adjust changelog * remove unnecessary multi_xml
1 parent 30e71e5 commit 53d826f

File tree

9 files changed

+30
-22
lines changed

9 files changed

+30
-22
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Your contribution here.
66

77
#### Fixes
8+
9+
* [#1995](https://github.com/ruby-grape/grape/pull/1995): Fix: "undefined instance variables" and "method redefined" warnings - [@nbeyer](https://github.com/nbeyer).
810
* [#1994](https://github.com/ruby-grape/grape/pull/1993): Fix typos in README - [@bellmyer](https://github.com/bellmyer).
911
* [#1993](https://github.com/ruby-grape/grape/pull/1993): Lazy join allow header - [@ericproulx](https://github.com/ericproulx).
1012
* [#1987](https://github.com/ruby-grape/grape/pull/1987): Re-add exactly_one_of mutually exclusive error message - [@ZeroInputCtrl](https://github.com/ZeroInputCtrl).
@@ -13,6 +15,7 @@
1315
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
1416
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
1517
* [#1988](https://github.com/ruby-grape/grape/pull/1988): Refactored the full_messages method and stop overriding full_message - [@hosseintoussi](https://github.com/hosseintoussi).
18+
1619
### 1.3.0 (2020/01/11)
1720

1821
#### Features

lib/grape/api.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Grape
88
# should subclass this class in order to build an API.
99
class API
1010
# Class methods that we want to call on the API rather than on the API object
11-
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile!]).freeze
11+
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile! inherited]).freeze
1212

1313
class << self
1414
attr_accessor :base_instance, :instances

lib/grape/dsl/helpers.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def api_changed(new_api)
9494
protected
9595

9696
def process_named_params
97-
return unless @named_params && @named_params.any?
97+
return unless instance_variable_defined?(:@named_params) && @named_params && @named_params.any?
9898
api.namespace_stackable(:named_params, @named_params)
9999
end
100100
end

lib/grape/dsl/inside_route.rb

+9-8
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ def status(status = nil)
182182
when Integer
183183
@status = status
184184
when nil
185-
return @status if @status
185+
return @status if instance_variable_defined?(:@status) && @status
186186
case request.request_method.to_s.upcase
187187
when Grape::Http::Headers::POST
188188
201
189189
when Grape::Http::Headers::DELETE
190-
if @body.present?
190+
if instance_variable_defined?(:@body) && @body.present?
191191
200
192192
else
193193
204
@@ -238,7 +238,7 @@ def body(value = nil)
238238
@body = ''
239239
status 204
240240
else
241-
@body
241+
instance_variable_defined?(:@body) ? @body : nil
242242
end
243243
end
244244

@@ -272,7 +272,7 @@ def file(value = nil)
272272
warn '[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.'
273273
@file = Grape::ServeFile::FileResponse.new(value)
274274
else
275-
@file
275+
instance_variable_defined?(:@file) ? @file : nil
276276
end
277277
end
278278

@@ -331,11 +331,12 @@ def present(*args)
331331
end
332332

333333
representation = { root => representation } if root
334+
334335
if key
335-
representation = (@body || {}).merge(key => representation)
336-
elsif entity_class.present? && @body
336+
representation = (body || {}).merge(key => representation)
337+
elsif entity_class.present? && body
337338
raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
338-
representation = @body.merge(representation)
339+
representation = body.merge(representation)
339340
end
340341

341342
body representation
@@ -387,7 +388,7 @@ def entity_class_for_obj(object, options)
387388
def entity_representation_for(entity_class, object, options)
388389
embeds = { env: env }
389390
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
390-
entity_class.represent(object, **embeds.merge(options))
391+
entity_class.represent(object, embeds.merge(options))
391392
end
392393
end
393394
end

lib/grape/dsl/parameters.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def requires(*attrs, &block)
127127

128128
opts = attrs.extract_options!.clone
129129
opts[:presence] = { value: true, message: opts[:message] }
130-
opts = @group.merge(opts) if @group
130+
opts = @group.merge(opts) if instance_variable_defined?(:@group) && @group
131131

132132
if opts[:using]
133133
require_required_and_optional_fields(attrs.first, opts)
@@ -146,7 +146,7 @@ def optional(*attrs, &block)
146146

147147
opts = attrs.extract_options!.clone
148148
type = opts[:type]
149-
opts = @group.merge(opts) if @group
149+
opts = @group.merge(opts) if instance_variable_defined?(:@group) && @group
150150

151151
# check type for optional parameter group
152152
if attrs && block_given?
@@ -243,8 +243,8 @@ def map_params(params, element)
243243
# @return hash of parameters relevant for the current scope
244244
# @api private
245245
def params(params)
246-
params = @parent.params(params) if @parent
247-
params = map_params(params, @element) if @element
246+
params = @parent.params(params) if instance_variable_defined?(:@parent) && @parent
247+
params = map_params(params, @element) if instance_variable_defined?(:@element) && @element
248248
params
249249
end
250250

lib/grape/dsl/routing.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def version(*args, &block)
5151
end
5252
end
5353

54-
@versions.last unless @versions.nil?
54+
@versions.last if instance_variable_defined?(:@versions) && @versions
5555
end
5656

5757
# Define a root URL prefix for your entire API.
@@ -163,6 +163,8 @@ def route(methods, paths = ['/'], route_options = {}, &block)
163163
# end
164164
# end
165165
def namespace(space = nil, options = {}, &block)
166+
@namespace_description = nil unless instance_variable_defined?(:@namespace_description) && @namespace_description
167+
166168
if space || block_given?
167169
within_namespace do
168170
previous_namespace_description = @namespace_description

spec/grape/api_spec.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ class DummyFormatClass
935935

936936
it 'adds a before filter to current and child namespaces only' do
937937
subject.get '/' do
938-
"root - #{@foo}"
938+
"root - #{instance_variable_defined?(:@foo) ? @foo : nil}"
939939
end
940940
subject.namespace :blah do
941941
before { @foo = 'foo' }
@@ -3677,12 +3677,13 @@ def before
36773677
end
36783678
end
36793679
context ':serializable_hash' do
3680-
before(:each) do
3681-
class SerializableHashExample
3682-
def serializable_hash
3683-
{ abc: 'def' }
3684-
end
3680+
class SerializableHashExample
3681+
def serializable_hash
3682+
{ abc: 'def' }
36853683
end
3684+
end
3685+
3686+
before(:each) do
36863687
subject.format :serializable_hash
36873688
end
36883689
it 'instance' do

spec/grape/validations/instance_behaivour_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let(:validator_type) do
77
Class.new(Grape::Validations::Base) do
88
def validate_param!(_attr_name, _params)
9-
if @instance_variable
9+
if instance_variable_defined?(:@instance_variable) && @instance_variable
1010
raise Grape::Exceptions::Validation.new(params: ['params'],
1111
message: 'This should never happen')
1212
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def read_chunks(body)
3535
config.include Spec::Support::Helpers
3636
config.raise_errors_for_deprecations!
3737
config.filter_run_when_matching :focus
38+
config.warnings = true
3839

3940
config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
4041

0 commit comments

Comments
 (0)