Skip to content

Rubocop bump #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AllCops:
Exclude:
- vendor/**/*
- Guardfile

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

# Offense count: 5
Metrics/CyclomaticComplexity:
Max: 17

# Offense count: 175
# Offense count: 176
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146
Expand All @@ -32,33 +32,11 @@ Metrics/MethodLength:
Metrics/PerceivedComplexity:
Max: 15

# Offense count: 2
# Cop supports --auto-correct.
Style/Blocks:
Enabled: false

# Offense count: 31
Style/Documentation:
Enabled: false

# Offense count: 3
Style/EachWithObject:
Enabled: false

# Offense count: 1
# Configuration parameters: Exclude.
Style/FileName:
Enabled: false

# Offense count: 16
Style/Lambda:
Enabled: false

# Offense count: 1
# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
Style/Next:
Enabled: false

# Offense count: 2
Style/RegexpLiteral:
MaxSlashes: 0
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ group :development, :test do
gem 'json'
gem 'rspec'
gem 'rack-test', '~> 0.6.2', require: 'rack/test'
gem 'rubocop', '0.28.0'
gem 'rubocop', '0.31.0'
end
47 changes: 21 additions & 26 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def self.expose(*args, &block)

@nested_attributes ||= []

# rubocop:disable Style/Next
args.each do |attribute|
unless @nested_attributes.empty?
orig_attribute = attribute.to_sym
Expand Down Expand Up @@ -233,14 +234,11 @@ def nested_exposures
# the values are document keys in the entity's documentation key. When calling
# #docmentation, any exposure without a documentation key will be ignored.
def self.documentation
@documentation ||= exposures.inject({}) do |memo, (attribute, exposure_options)|
@documentation ||= exposures.each_with_object({}) do |(attribute, exposure_options), memo|
unless exposure_options[:documentation].nil? || exposure_options[:documentation].empty?
memo[key_for(attribute)] = exposure_options[:documentation]
end
memo
end

@documentation
end

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

def initialize(object, options = {})
@object, @options = object, options
@object = object
@options = options
end

def exposures
Expand Down Expand Up @@ -468,25 +467,23 @@ def serializable_hash(runtime_options = {})

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

valid_exposures.inject({}) do |output, (attribute, exposure_options)|
if should_return_attribute?(attribute, opts) && conditions_met?(exposure_options, opts)
partial_output = value_for(attribute, opts)

output[self.class.key_for(attribute)] =
if partial_output.respond_to?(:serializable_hash)
partial_output.serializable_hash(runtime_options)
elsif partial_output.is_a?(Array) && !partial_output.map { |o| o.respond_to?(:serializable_hash) }.include?(false)
partial_output.map(&:serializable_hash)
elsif partial_output.is_a?(Hash)
partial_output.each do |key, value|
partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
end
else
partial_output
end
end
valid_exposures.each_with_object({}) do |(attribute, exposure_options), output|
next unless should_return_attribute?(attribute, opts) && conditions_met?(exposure_options, opts)

partial_output = value_for(attribute, opts)

output
output[self.class.key_for(attribute)] =
if partial_output.respond_to?(:serializable_hash)
partial_output.serializable_hash(runtime_options)
elsif partial_output.is_a?(Array) && !partial_output.map { |o| o.respond_to?(:serializable_hash) }.include?(false)
partial_output.map(&:serializable_hash)
elsif partial_output.is_a?(Hash)
partial_output.each do |key, value|
partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
end
else
partial_output
end
end
end

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

@only_fields ||= options[:only].inject({}) do |allowed_fields, attribute|
@only_fields ||= options[:only].each_with_object({}) do |attribute, allowed_fields|
if attribute.is_a?(Hash)
attribute.each do |attr, nested_attrs|
allowed_fields[attr] ||= []
Expand All @@ -507,8 +504,6 @@ def only_fields(options, for_attribute = nil)
else
allowed_fields[attribute] = true
end

allowed_fields
end

if for_attribute && @only_fields[for_attribute].is_a?(Array)
Expand Down
40 changes: 20 additions & 20 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class BogusEntity < Grape::Entity

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

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

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

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

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

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

Expand Down Expand Up @@ -364,7 +364,7 @@ class Parent < Person
end

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

subject.class_eval do
# Symbol
Expand All @@ -389,7 +389,7 @@ class Parent < Person
end

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

subject.class_eval do
# Symbol
Expand Down Expand Up @@ -433,10 +433,10 @@ class Parent < Person
end

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

subject.class_eval do
with_options(proc: lambda { |_obj, _opts| 'awesome' }) do
with_options(proc: ->(_obj, _opts) { 'awesome' }) do
expose :awesome_thing, proc: match_proc
end
end
Expand Down Expand Up @@ -773,8 +773,8 @@ class Parent < Person

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

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

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

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

expose :fantasies, format_with: lambda { |f| f.reverse }
expose :fantasies, format_with: ->(f) { f.reverse }
end
end

Expand Down Expand Up @@ -1238,7 +1238,7 @@ class UserEntity < Grape::Entity
end

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

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

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

expect(subject.send(:conditions_met?, exposure_options, true: false)).to be true
expect(subject.send(:conditions_met?, exposure_options, true: true)).to be false
Expand Down