Skip to content

Commit 00328f1

Browse files
committed
Merge pull request #132 from etehtsea/rubocop-bump
Rubocop bump
2 parents a67a176 + 5d1f2e6 commit 00328f1

File tree

5 files changed

+46
-72
lines changed

5 files changed

+46
-72
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
AllCops:
22
Exclude:
33
- vendor/**/*
4+
- Guardfile
45

56
inherit_from: .rubocop_todo.yml

.rubocop_todo.yml

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-22 20:12:08 -0300 using RuboCop version 0.28.0.
2+
# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
@@ -12,13 +12,13 @@ Metrics/AbcSize:
1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 337
15+
Max: 328
1616

1717
# Offense count: 5
1818
Metrics/CyclomaticComplexity:
1919
Max: 17
2020

21-
# Offense count: 175
21+
# Offense count: 176
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 146
@@ -32,33 +32,11 @@ Metrics/MethodLength:
3232
Metrics/PerceivedComplexity:
3333
Max: 15
3434

35-
# Offense count: 2
36-
# Cop supports --auto-correct.
37-
Style/Blocks:
38-
Enabled: false
39-
4035
# Offense count: 31
4136
Style/Documentation:
4237
Enabled: false
4338

44-
# Offense count: 3
45-
Style/EachWithObject:
46-
Enabled: false
47-
4839
# Offense count: 1
4940
# Configuration parameters: Exclude.
5041
Style/FileName:
5142
Enabled: false
52-
53-
# Offense count: 16
54-
Style/Lambda:
55-
Enabled: false
56-
57-
# Offense count: 1
58-
# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
59-
Style/Next:
60-
Enabled: false
61-
62-
# Offense count: 2
63-
Style/RegexpLiteral:
64-
MaxSlashes: 0

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ group :development, :test do
1212
gem 'json'
1313
gem 'rspec'
1414
gem 'rack-test', '~> 0.6.2', require: 'rack/test'
15-
gem 'rubocop', '0.28.0'
15+
gem 'rubocop', '0.31.0'
1616
end

lib/grape_entity/entity.rb

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def self.expose(*args, &block)
137137

138138
@nested_attributes ||= []
139139

140+
# rubocop:disable Style/Next
140141
args.each do |attribute|
141142
unless @nested_attributes.empty?
142143
orig_attribute = attribute.to_sym
@@ -233,14 +234,11 @@ def nested_exposures
233234
# the values are document keys in the entity's documentation key. When calling
234235
# #docmentation, any exposure without a documentation key will be ignored.
235236
def self.documentation
236-
@documentation ||= exposures.inject({}) do |memo, (attribute, exposure_options)|
237+
@documentation ||= exposures.each_with_object({}) do |(attribute, exposure_options), memo|
237238
unless exposure_options[:documentation].nil? || exposure_options[:documentation].empty?
238239
memo[key_for(attribute)] = exposure_options[:documentation]
239240
end
240-
memo
241241
end
242-
243-
@documentation
244242
end
245243

246244
# This allows you to declare a Proc in which exposures can be formatted with.
@@ -435,7 +433,8 @@ def presented
435433
end
436434

437435
def initialize(object, options = {})
438-
@object, @options = object, options
436+
@object = object
437+
@options = options
439438
end
440439

441440
def exposures
@@ -468,25 +467,23 @@ def serializable_hash(runtime_options = {})
468467

469468
opts = options.merge(runtime_options || {})
470469

471-
valid_exposures.inject({}) do |output, (attribute, exposure_options)|
472-
if should_return_attribute?(attribute, opts) && conditions_met?(exposure_options, opts)
473-
partial_output = value_for(attribute, opts)
474-
475-
output[self.class.key_for(attribute)] =
476-
if partial_output.respond_to?(:serializable_hash)
477-
partial_output.serializable_hash(runtime_options)
478-
elsif partial_output.is_a?(Array) && !partial_output.map { |o| o.respond_to?(:serializable_hash) }.include?(false)
479-
partial_output.map(&:serializable_hash)
480-
elsif partial_output.is_a?(Hash)
481-
partial_output.each do |key, value|
482-
partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
483-
end
484-
else
485-
partial_output
486-
end
487-
end
470+
valid_exposures.each_with_object({}) do |(attribute, exposure_options), output|
471+
next unless should_return_attribute?(attribute, opts) && conditions_met?(exposure_options, opts)
472+
473+
partial_output = value_for(attribute, opts)
488474

489-
output
475+
output[self.class.key_for(attribute)] =
476+
if partial_output.respond_to?(:serializable_hash)
477+
partial_output.serializable_hash(runtime_options)
478+
elsif partial_output.is_a?(Array) && !partial_output.map { |o| o.respond_to?(:serializable_hash) }.include?(false)
479+
partial_output.map(&:serializable_hash)
480+
elsif partial_output.is_a?(Hash)
481+
partial_output.each do |key, value|
482+
partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
483+
end
484+
else
485+
partial_output
486+
end
490487
end
491488
end
492489

@@ -498,7 +495,7 @@ def should_return_attribute?(attribute, options)
498495
def only_fields(options, for_attribute = nil)
499496
return nil unless options[:only]
500497

501-
@only_fields ||= options[:only].inject({}) do |allowed_fields, attribute|
498+
@only_fields ||= options[:only].each_with_object({}) do |attribute, allowed_fields|
502499
if attribute.is_a?(Hash)
503500
attribute.each do |attr, nested_attrs|
504501
allowed_fields[attr] ||= []
@@ -507,8 +504,6 @@ def only_fields(options, for_attribute = nil)
507504
else
508505
allowed_fields[attribute] = true
509506
end
510-
511-
allowed_fields
512507
end
513508

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

spec/grape_entity/entity_spec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BogusEntity < Grape::Entity
7070

7171
context 'with parameters passed to the block' do
7272
it 'sets the :proc option in the exposure options' do
73-
block = lambda { |_| true }
73+
block = ->(_) { true }
7474
subject.expose :name, using: 'Awesome', &block
7575
expect(subject.exposures[:name]).to eq(proc: block, using: 'Awesome')
7676
end
@@ -112,8 +112,8 @@ class BogusEntity < Grape::Entity
112112

113113
it 'does not represent nested exposures whose conditions are not met' do
114114
subject.expose :awesome do
115-
subject.expose(:condition_met, if: lambda { |_, _| true }) { |_| 'value' }
116-
subject.expose(:condition_not_met, if: lambda { |_, _| false }) { |_| 'value' }
115+
subject.expose(:condition_met, if: ->(_, _) { true }) { |_| 'value' }
116+
subject.expose(:condition_not_met, if: ->(_, _) { false }) { |_| 'value' }
117117
end
118118

119119
expect(subject.represent({}).send(:value_for, :awesome)).to eq(condition_met: 'value')
@@ -228,7 +228,7 @@ class Parent < Person
228228
end
229229

230230
context 'register formatters' do
231-
let(:date_formatter) { lambda { |date| date.strftime('%m/%d/%Y') } }
231+
let(:date_formatter) { ->(date) { date.strftime('%m/%d/%Y') } }
232232

233233
it 'registers a formatter' do
234234
subject.format_with :timestamp, &date_formatter
@@ -261,7 +261,7 @@ class Parent < Person
261261
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
262262
object = {}
263263

264-
subject.expose(:size, format_with: lambda { |_value| self.object.class.to_s })
264+
subject.expose(:size, format_with: ->(_value) { self.object.class.to_s })
265265
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
266266
end
267267

@@ -364,7 +364,7 @@ class Parent < Person
364364
end
365365

366366
it 'merges nested :if option' do
367-
match_proc = lambda { |_obj, _opts| true }
367+
match_proc = ->(_obj, _opts) { true }
368368

369369
subject.class_eval do
370370
# Symbol
@@ -389,7 +389,7 @@ class Parent < Person
389389
end
390390

391391
it 'merges nested :unless option' do
392-
match_proc = lambda { |_, _| true }
392+
match_proc = ->(_, _) { true }
393393

394394
subject.class_eval do
395395
# Symbol
@@ -433,10 +433,10 @@ class Parent < Person
433433
end
434434

435435
it 'overrides nested :proc option' do
436-
match_proc = lambda { |_obj, _opts| 'more awesomer' }
436+
match_proc = ->(_obj, _opts) { 'more awesomer' }
437437

438438
subject.class_eval do
439-
with_options(proc: lambda { |_obj, _opts| 'awesome' }) do
439+
with_options(proc: ->(_obj, _opts) { 'awesome' }) do
440440
expose :awesome_thing, proc: match_proc
441441
end
442442
end
@@ -773,8 +773,8 @@ class Parent < Person
773773

774774
it "does not expose attributes that don't exist on the object, even with criteria" do
775775
fresh_class.expose :email
776-
fresh_class.expose :nonexistent_attribute, safe: true, if: lambda { false }
777-
fresh_class.expose :nonexistent_attribute2, safe: true, if: lambda { true }
776+
fresh_class.expose :nonexistent_attribute, safe: true, if: -> { false }
777+
fresh_class.expose :nonexistent_attribute2, safe: true, if: -> { true }
778778

779779
res = fresh_class.new(model).serializable_hash
780780
expect(res).to have_key :email
@@ -798,9 +798,9 @@ class Parent < Person
798798
end
799799

800800
it 'does not expose attributes that are generated by a block but have not passed criteria' do
801-
fresh_class.expose :nonexistent_attribute, proc: lambda { |_model, _opts|
802-
'I exist, but it is not yet my time to shine'
803-
}, if: lambda { |_model, _opts| false }
801+
fresh_class.expose :nonexistent_attribute,
802+
proc: ->(_model, _opts) { 'I exist, but it is not yet my time to shine' },
803+
if: ->(_model, _opts) { false }
804804
res = fresh_class.new(model).serializable_hash
805805
expect(res).not_to have_key :nonexistent_attribute
806806
end
@@ -820,9 +820,9 @@ class TestEntity < Grape::Entity
820820
end
821821

822822
it 'does not expose attributes that are generated by a block but have not passed criteria' do
823-
fresh_class.expose :nonexistent_attribute, proc: lambda { |_, _|
824-
'I exist, but it is not yet my time to shine'
825-
}, if: lambda { |_, _| false }
823+
fresh_class.expose :nonexistent_attribute,
824+
proc: ->(_, _) { 'I exist, but it is not yet my time to shine' },
825+
if: ->(_, _) { false }
826826
res = fresh_class.new(model).serializable_hash
827827
expect(res).not_to have_key :nonexistent_attribute
828828
end
@@ -901,7 +901,7 @@ def timestamp(date)
901901
date.strftime('%m/%d/%Y')
902902
end
903903

904-
expose :fantasies, format_with: lambda { |f| f.reverse }
904+
expose :fantasies, format_with: ->(f) { f.reverse }
905905
end
906906
end
907907

@@ -1238,7 +1238,7 @@ class UserEntity < Grape::Entity
12381238
end
12391239

12401240
it 'only passes through proc :if exposure if it returns truthy value' do
1241-
exposure_options = { if: lambda { |_, opts| opts[:true] } }
1241+
exposure_options = { if: ->(_, opts) { opts[:true] } }
12421242

12431243
expect(subject.send(:conditions_met?, exposure_options, true: false)).to be false
12441244
expect(subject.send(:conditions_met?, exposure_options, true: true)).to be true
@@ -1256,7 +1256,7 @@ class UserEntity < Grape::Entity
12561256
end
12571257

12581258
it 'only passes through proc :unless exposure if it returns falsy value' do
1259-
exposure_options = { unless: lambda { |_, options| options[:true] == true } }
1259+
exposure_options = { unless: ->(_, opts) { opts[:true] == true } }
12601260

12611261
expect(subject.send(:conditions_met?, exposure_options, true: false)).to be true
12621262
expect(subject.send(:conditions_met?, exposure_options, true: true)).to be false

0 commit comments

Comments
 (0)