Skip to content

Commit 22bc016

Browse files
committed
Replace reduce hash with each_with_object
1 parent 14ba8da commit 22bc016

File tree

2 files changed

+3
-14
lines changed

2 files changed

+3
-14
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ Metrics/PerceivedComplexity:
3636
Style/Documentation:
3737
Enabled: false
3838

39-
# Offense count: 3
40-
Style/EachWithObject:
41-
Enabled: false
42-
4339
# Offense count: 1
4440
# Configuration parameters: Exclude.
4541
Style/FileName:

lib/grape_entity/entity.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,11 @@ def nested_exposures
233233
# the values are document keys in the entity's documentation key. When calling
234234
# #docmentation, any exposure without a documentation key will be ignored.
235235
def self.documentation
236-
@documentation ||= exposures.inject({}) do |memo, (attribute, exposure_options)|
236+
@documentation ||= exposures.each_with_object({}) do |(attribute, exposure_options), memo|
237237
unless exposure_options[:documentation].nil? || exposure_options[:documentation].empty?
238238
memo[key_for(attribute)] = exposure_options[:documentation]
239239
end
240-
memo
241240
end
242-
243-
@documentation
244241
end
245242

246243
# This allows you to declare a Proc in which exposures can be formatted with.
@@ -468,7 +465,7 @@ def serializable_hash(runtime_options = {})
468465

469466
opts = options.merge(runtime_options || {})
470467

471-
valid_exposures.inject({}) do |output, (attribute, exposure_options)|
468+
valid_exposures.each_with_object({}) do |(attribute, exposure_options), output|
472469
if should_return_attribute?(attribute, opts) && conditions_met?(exposure_options, opts)
473470
partial_output = value_for(attribute, opts)
474471

@@ -485,8 +482,6 @@ def serializable_hash(runtime_options = {})
485482
partial_output
486483
end
487484
end
488-
489-
output
490485
end
491486
end
492487

@@ -498,7 +493,7 @@ def should_return_attribute?(attribute, options)
498493
def only_fields(options, for_attribute = nil)
499494
return nil unless options[:only]
500495

501-
@only_fields ||= options[:only].inject({}) do |allowed_fields, attribute|
496+
@only_fields ||= options[:only].each_with_object({}) do |attribute, allowed_fields|
502497
if attribute.is_a?(Hash)
503498
attribute.each do |attr, nested_attrs|
504499
allowed_fields[attr] ||= []
@@ -507,8 +502,6 @@ def only_fields(options, for_attribute = nil)
507502
else
508503
allowed_fields[attribute] = true
509504
end
510-
511-
allowed_fields
512505
end
513506

514507
if for_attribute && @only_fields[for_attribute].is_a?(Array)

0 commit comments

Comments
 (0)