From 1443468f6593360b44d2ca6da5f03b19e51a2be1 Mon Sep 17 00:00:00 2001 From: LeFnord Date: Fri, 15 Mar 2019 00:23:03 +0100 Subject: [PATCH] Updates travis matrix ... - changed to some rubocop suggestions --- .rubocop.yml | 55 +++++++++++-------- .rubocop_todo.yml | 55 ++++--------------- .travis.yml | 5 +- Gemfile | 2 +- Guardfile | 6 +- grape-entity.gemspec | 2 +- lib/grape_entity/entity.rb | 22 +++++++- lib/grape_entity/exposure/base.rb | 5 +- lib/grape_entity/exposure/nesting_exposure.rb | 1 + .../nesting_exposure/nested_exposures.rb | 2 +- .../nesting_exposure/output_builder.rb | 1 + spec/grape_entity/entity_spec.rb | 2 +- .../exposure/represent_exposure_spec.rb | 6 +- 13 files changed, 85 insertions(+), 79 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1536477a..58ded130 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,37 +1,48 @@ +inherit_from: .rubocop_todo.yml + AllCops: + Exclude: + - vendor/**/* + - example/**/* TargetRubyVersion: 2.4 - Include: - - Dangerfile +Layout/EmptyLinesAroundArguments: + Enabled: false + +Layout/IndentHash: + EnforcedStyle: consistent + +Metrics/AbcSize: + Max: 25 + +Metrics/BlockLength: Exclude: - - vendor/**/* - - bin/**/* - - Guardfile + - spec/**/* -inherit_from: .rubocop_todo.yml +Metrics/CyclomaticComplexity: + Max: 10 -Gemspec/RequiredRubyVersion: - Enabled: false +Metrics/ClassLength: + Max: 300 -Naming/FileName: +Metrics/LineLength: + Max: 120 Exclude: - - 'Gemfile' - - 'Rakefile' - - 'grape-entity.gemspec' - - 'lib/grape-entity.rb' + - spec/**/* -Style/Documentation: - Enabled: false +Metrics/MethodLength: + Max: 26 + Exclude: + - spec/**/* -Style/MultilineIfModifier: - Enabled: false +Metrics/PerceivedComplexity: + Max: 11 -Style/RaiseArgs: +Naming: Enabled: false -Lint/BooleanSymbol: - Exclude: - - 'spec/grape_entity/exposure_spec.rb' +Style/Documentation: + Enabled: false -Lint/UnneededDisable: +Style/RegexpLiteral: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7fa1f9da..7f2f6665 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,55 +1,30 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-03-28 21:26:18 +0200 using RuboCop version 0.54.0. +# on 2019-03-15 00:00:57 +0100 using RuboCop version 0.65.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 # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -Lint/AmbiguousBlockAssociation: +# Offense count: 6 +Lint/BooleanSymbol: Exclude: - - 'spec/grape_entity/exposure/represent_exposure_spec.rb' - -# Offense count: 5 -Metrics/AbcSize: - Max: 25 + - 'spec/grape_entity/exposure_spec.rb' -# Offense count: 39 -# Configuration parameters: CountComments, ExcludedMethods. -Metrics/BlockLength: - Max: 1632 -# Offense count: 1 -# Configuration parameters: CountComments. -Metrics/ClassLength: - Max: 205 - -# Offense count: 1 -Metrics/CyclomaticComplexity: - Max: 10 - -# Offense count: 6 -# Configuration parameters: CountComments. -Metrics/MethodLength: - Max: 26 +# Offense count: 4 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 146 # Offense count: 1 -Metrics/PerceivedComplexity: - Max: 11 - -# Offense count: 3 -# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. -# AllowedNames: io, id, to -Naming/UncommunicativeMethodParamName: +# Configuration parameters: EnforcedStyle. +# SupportedStyles: inline, group +Style/AccessModifierDeclarations: Exclude: - 'spec/grape_entity/entity_spec.rb' -# Offense count: 1 -Style/EvalWithLocation: - Exclude: - - 'lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: IgnoredMethods. @@ -57,9 +32,3 @@ Style/EvalWithLocation: Style/SymbolProc: Exclude: - 'spec/grape_entity/entity_spec.rb' - -# Offense count: 272 -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. -# URISchemes: http, https -Metrics/LineLength: - Max: 146 diff --git a/.travis.yml b/.travis.yml index f36d1f24..258a513c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,9 @@ after_success: - bundle exec danger rvm: - - 2.3.8 - 2.4.5 - - 2.5.3 - - 2.6.0 + - 2.5.4 + - 2.6.2 - ruby-head - jruby-head diff --git a/Gemfile b/Gemfile index 64cbc840..e9e08b61 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source 'http://rubygems.org' gemspec group :development, :test do - gem 'rubocop', '~> 0.61', require: false + gem 'rubocop', '~> 0.65', require: false end group :test do diff --git a/Guardfile b/Guardfile index 65795c10..f6fde9f9 100644 --- a/Guardfile +++ b/Guardfile @@ -1,11 +1,13 @@ +# frozen_string_literal: true + # A sample Guardfile # More info at https://github.com/guard/guard#readme guard 'rspec', version: 2 do watch(%r{^spec/.+_spec\.rb$}) - watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^spec/support/shared_versioning_examples.rb$}) { |_m| 'spec/' } - watch('spec/spec_helper.rb') { 'spec/' } + watch('spec/spec_helper.rb') { 'spec/' } end guard 'bundler' do diff --git a/grape-entity.gemspec b/grape-entity.gemspec index 63d6a72e..fe754907 100644 --- a/grape-entity.gemspec +++ b/grape-entity.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.description = 'Extracted from Grape, A Ruby framework for rapid API development with great conventions.' s.license = 'MIT' - s.required_ruby_version = '>= 2.3' + s.required_ruby_version = '>= 2.4' s.rubyforge_project = 'grape-entity' diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index fd509956..07ee884e 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -168,10 +168,13 @@ def self.inherited(subclass) # @option options :documentation Define documenation for an exposed # field, typically the value is a hash with two fields, type and desc. # @option options :merge This option allows you to merge an exposed field to the root + # + # rubocop:disable Metrics/LineLength def self.expose(*args, &block) options = merge_options(args.last.is_a?(Hash) ? args.pop : {}) if args.size > 1 + raise ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as] raise ArgumentError, 'You may not use the :expose_nil on multi-attribute exposures.' if options.key?(:expose_nil) raise ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given? @@ -191,6 +194,7 @@ def self.expose(*args, &block) @nesting_stack ||= [] args.each { |attribute| build_exposure_for_attribute(attribute, @nesting_stack, options, block) } end + # rubocop:enable Metrics/LineLength def self.build_exposure_for_attribute(attribute, nesting_stack, options, block) exposure_list = nesting_stack.empty? ? root_exposures : nesting_stack.last.nested_exposures @@ -291,6 +295,7 @@ def self.documentation # def self.format_with(name, &block) raise ArgumentError, 'You must pass a block for formatters' unless block_given? + formatters[name.to_sym] = block end @@ -534,7 +539,22 @@ def to_xml(options = {}) # All supported options. OPTIONS = %i[ - rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil override + rewrite + as + if + unless + using + with + proc + documentation + format_with + safe + attr_path + if_extras + unless_extras + merge + expose_nil + override ].to_set.freeze # Merges the given options with current block options. diff --git a/lib/grape_entity/exposure/base.rb b/lib/grape_entity/exposure/base.rb index 41494496..b2aaf426 100644 --- a/lib/grape_entity/exposure/base.rb +++ b/lib/grape_entity/exposure/base.rb @@ -54,7 +54,10 @@ def valid?(entity) if @is_safe is_delegatable else - is_delegatable || raise(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}") + is_delegatable || raise( + NoMethodError, + "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}" + ) end end diff --git a/lib/grape_entity/exposure/nesting_exposure.rb b/lib/grape_entity/exposure/nesting_exposure.rb index 57d30cb7..8877ba95 100644 --- a/lib/grape_entity/exposure/nesting_exposure.rb +++ b/lib/grape_entity/exposure/nesting_exposure.rb @@ -87,6 +87,7 @@ def normalized_exposures(entity, options) exposure.should_expose?(entity, options) end next unless should_expose + output[exposure.key(entity)] ||= [] output[exposure.key(entity)] << exposure end diff --git a/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb b/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb index c13010e6..b1f40711 100644 --- a/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb +++ b/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb @@ -49,7 +49,7 @@ def clear length empty? ].each do |name| - class_eval <<-RUBY, __FILE__, __LINE__ + class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name}(*args, &block) @exposures.#{name}(*args, &block) end diff --git a/lib/grape_entity/exposure/nesting_exposure/output_builder.rb b/lib/grape_entity/exposure/nesting_exposure/output_builder.rb index 3e1e3a8e..aef3e98f 100644 --- a/lib/grape_entity/exposure/nesting_exposure/output_builder.rb +++ b/lib/grape_entity/exposure/nesting_exposure/output_builder.rb @@ -19,6 +19,7 @@ def add(exposure, result) # If we have an array which should not be merged - save it with a key as a hash # If we have hash which should be merged - save it without a key (merge) return unless result + @output_hash.merge! result, &merge_strategy(exposure.for_merge) else @output_hash[exposure.key(@entity)] = result diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index e0c6388a..41bed2d9 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1376,7 +1376,7 @@ class TestEntity < Grape::Entity expect(res).to have_key :nonexistent_attribute end - it "exposes attributes defined through module inclusion" do + it 'exposes attributes defined through module inclusion' do module SharedAttributes def a_value 3.14 diff --git a/spec/grape_entity/exposure/represent_exposure_spec.rb b/spec/grape_entity/exposure/represent_exposure_spec.rb index 4da76a83..95847cbf 100644 --- a/spec/grape_entity/exposure/represent_exposure_spec.rb +++ b/spec/grape_entity/exposure/represent_exposure_spec.rb @@ -12,11 +12,11 @@ let(:subexposure) { double(:subexposure) } it 'sets using_class_name' do - expect { subject }.to change { exposure.using_class_name }.to(using_class_name) + expect { subject }.to change(exposure, :using_class_name).to(using_class_name) end it 'sets subexposure' do - expect { subject }.to change { exposure.subexposure }.to(subexposure) + expect { subject }.to change(exposure, :subexposure).to(subexposure) end context 'when using_class is set' do @@ -25,7 +25,7 @@ end it 'resets using_class' do - expect { subject }.to change { exposure.using_class } + expect { subject }.to change(exposure, :using_class) end end end