Skip to content

Commit ddc3b8c

Browse files
authored
Merge pull request #2656 from ruby-grape/remove_instance_variable_defined
Remove useless instance_variable_defined? checks
2 parents 519029a + 963b79c commit ddc3b8c

6 files changed

Lines changed: 11 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#2656](https://github.com/ruby-grape/grape/pull/2656): Remove useless instance_variable_defined? checks - [@ericproulx](https://github.com/ericproulx).
56
* Your contribution here.
67

78
#### Fixes

lib/grape/dsl/inside_route.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ def status(status = nil)
7070
when Integer
7171
@status = status
7272
when nil
73-
return @status if instance_variable_defined?(:@status) && @status
73+
return @status if @status
7474

7575
if request.post?
7676
201
7777
elsif request.delete?
78-
if instance_variable_defined?(:@body) && @body.present?
78+
if @body.present?
7979
200
8080
else
8181
204
@@ -114,7 +114,7 @@ def body(value = nil)
114114
@body = ''
115115
status 204
116116
else
117-
instance_variable_defined?(:@body) ? @body : nil
117+
@body
118118
end
119119
end
120120

lib/grape/dsl/parameters.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def use(*names, **options)
124124
# end
125125
def requires(*attrs, **opts, &block)
126126
opts[:presence] = { value: true, message: opts[:message] }
127-
opts = @group.deep_merge(opts) if instance_variable_defined?(:@group) && @group
127+
opts = @group.deep_merge(opts) if @group
128128

129129
if opts[:using]
130130
require_required_and_optional_fields(attrs.first, opts)
@@ -140,7 +140,7 @@ def requires(*attrs, **opts, &block)
140140
# @option (see #requires)
141141
def optional(*attrs, **opts, &block)
142142
type = opts[:type]
143-
opts = @group.deep_merge(opts) if instance_variable_defined?(:@group) && @group
143+
opts = @group.deep_merge(opts) if @group
144144

145145
# check type for optional parameter group
146146
if attrs && block
@@ -224,8 +224,8 @@ def map_params(params, element, is_array = false)
224224
# @return hash of parameters relevant for the current scope
225225
# @api private
226226
def params(params)
227-
params = @parent.params_meeting_dependency.presence || @parent.params(params) if instance_variable_defined?(:@parent) && @parent
228-
params = map_params(params, @element) if instance_variable_defined?(:@element) && @element
227+
params = @parent.params_meeting_dependency.presence || @parent.params(params) if @parent
228+
params = map_params(params, @element) if @element
229229
params
230230
end
231231

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def version(*args, **options, &block)
6060
end
6161
end
6262

63-
@versions.last if instance_variable_defined?(:@versions) && @versions
63+
@versions&.last
6464
end
6565

6666
# Define a root URL prefix for your entire API.

spec/grape/api/custom_validations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def access_header
216216
let(:validator_type) do
217217
Class.new(Grape::Validations::Validators::Base) do
218218
def validate_param!(_attr_name, _params)
219-
if instance_variable_defined?(:@instance_variable) && @instance_variable
219+
if @instance_variable
220220
raise Grape::Exceptions::Validation.new(params: ['params'],
221221
message: 'This should never happen')
222222
end

spec/grape/api_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ def to_txt
10691069

10701070
it 'adds a before filter to current and child namespaces only' do
10711071
subject.get '/' do
1072-
"root - #{@foo if instance_variable_defined?(:@foo)}"
1072+
"root - #{@foo if @foo}"
10731073
end
10741074
subject.namespace :blah do
10751075
before { @foo = 'foo' }

0 commit comments

Comments
 (0)