From 3ac27f74114e64f552a68a05532d0319b8dc7f1c Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Sat, 27 Jul 2019 18:15:09 +0200 Subject: [PATCH] Make code compliant to rubocop 0.73 --- .rubocop.yml | 2 ++ lib/hanami/entity/schema.rb | 8 ++++---- lib/hanami/model/associations/has_many.rb | 8 ++++---- lib/hanami/model/associations/has_one.rb | 12 ++++++------ lib/hanami/model/associations/many_to_many.rb | 4 ++-- lib/hanami/model/configuration.rb | 10 +++++----- lib/hanami/model/mapped_relation.rb | 4 ++-- lib/hanami/model/migrator/adapter.rb | 8 ++++---- lib/hanami/model/migrator/mysql_adapter.rb | 12 ++++++------ lib/hanami/model/migrator/postgres_adapter.rb | 4 ++-- lib/hanami/repository.rb | 16 ++++++++-------- spec/support/platform/matcher.rb | 2 +- 12 files changed, 46 insertions(+), 44 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 93919b73..927223c1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,8 @@ # alphabetically inherit_from: - https://raw.githubusercontent.com/hanami/devtools/master/.rubocop.yml +Naming/RescuedExceptionsVariableName: + PreferredName: "exception" Style/RescueStandardError: Enabled: false Style/DateTime: diff --git a/lib/hanami/entity/schema.rb b/lib/hanami/entity/schema.rb index befac8d1..9678ec84 100644 --- a/lib/hanami/entity/schema.rb +++ b/lib/hanami/entity/schema.rb @@ -182,10 +182,10 @@ def initialize(type = nil, &blk) # @api private def call(attributes) schema.call(attributes) - rescue Dry::Types::SchemaError => e - raise TypeError.new(e.message) - rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError => e - raise ArgumentError.new(e.message) + rescue Dry::Types::SchemaError => exception + raise TypeError.new(exception.message) + rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError => exception + raise ArgumentError.new(exception.message) end # Check if the attribute is known diff --git a/lib/hanami/model/associations/has_many.rb b/lib/hanami/model/associations/has_many.rb index 821b0702..f8724237 100644 --- a/lib/hanami/model/associations/has_many.rb +++ b/lib/hanami/model/associations/has_many.rb @@ -51,8 +51,8 @@ def initialize(repository, source, target, subject, scope = nil) def create(data) entity.new(command(:create, aggregate(target), mapper: nil, use: [:timestamps]) .call(serialize(data))) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # @since 0.7.0 @@ -60,8 +60,8 @@ def create(data) def add(data) command(:create, relation(target), use: [:timestamps]) .call(associate(serialize(data))) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # @since 0.7.0 diff --git a/lib/hanami/model/associations/has_one.rb b/lib/hanami/model/associations/has_one.rb index 67a712e6..d4640386 100644 --- a/lib/hanami/model/associations/has_one.rb +++ b/lib/hanami/model/associations/has_one.rb @@ -53,14 +53,14 @@ def create(data) entity.new( command(:create, aggregate(target), mapper: nil).call(serialize(data)) ) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end def add(data) command(:create, relation(target), mapper: nil).call(associate(serialize(data))) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end def update(data) @@ -68,8 +68,8 @@ def update(data) .by_pk( one.public_send(relation(target).primary_key) ).call(serialize(data)) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end def delete diff --git a/lib/hanami/model/associations/many_to_many.rb b/lib/hanami/model/associations/many_to_many.rb index 614dce19..52c0fc98 100644 --- a/lib/hanami/model/associations/many_to_many.rb +++ b/lib/hanami/model/associations/many_to_many.rb @@ -76,8 +76,8 @@ def where(condition) def add(*data) command(:create, relation(through), use: [:timestamps]) .call(associate(serialize(data))) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # @since 1.1.0 diff --git a/lib/hanami/model/configuration.rb b/lib/hanami/model/configuration.rb index 3b0e6a70..2cadbcce 100644 --- a/lib/hanami/model/configuration.rb +++ b/lib/hanami/model/configuration.rb @@ -136,10 +136,10 @@ def logger=(value) # @api private def rom @rom ||= ROM::Configuration.new(@backend, @url, infer_relations: false) - rescue => e - raise UnknownDatabaseAdapterError.new(@url) if e.message =~ /adapters/ + rescue => exception + raise UnknownDatabaseAdapterError.new(@url) if exception.message =~ /adapters/ - raise e + raise exception end # @raise [Hanami::Model::UnknownDatabaseAdapterError] if `url` is blank, @@ -157,8 +157,8 @@ def load!(repositories, &blk) # rubocop:disable Metrics/AbcSize container = ROM.container(rom) define_entities_mappings(container, repositories) container - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # @since 1.0.0 diff --git a/lib/hanami/model/mapped_relation.rb b/lib/hanami/model/mapped_relation.rb index e15f2d23..70e260b4 100644 --- a/lib/hanami/model/mapped_relation.rb +++ b/lib/hanami/model/mapped_relation.rb @@ -51,8 +51,8 @@ def initialize(relation) # end def [](attribute) @relation[attribute] - rescue KeyError => e - raise UnknownAttributeError.new(e.message) + rescue KeyError => exception + raise UnknownAttributeError.new(exception.message) end end end diff --git a/lib/hanami/model/migrator/adapter.rb b/lib/hanami/model/migrator/adapter.rb index 7488fbc3..589305b5 100644 --- a/lib/hanami/model/migrator/adapter.rb +++ b/lib/hanami/model/migrator/adapter.rb @@ -80,8 +80,8 @@ def migrate(migrations, version) version = Integer(version) unless version.nil? Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true) - rescue Sequel::Migrator::Error => e - raise MigrationError.new(e.message) + rescue Sequel::Migrator::Error => exception + raise MigrationError.new(exception.message) end # @since 1.1.0 @@ -91,8 +91,8 @@ def rollback(migrations, steps) version = version_to_rollback(table, steps) Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true) - rescue Sequel::Migrator::Error => e - raise MigrationError.new(e.message) + rescue Sequel::Migrator::Error => exception + raise MigrationError.new(exception.message) end # Load database schema. diff --git a/lib/hanami/model/migrator/mysql_adapter.rb b/lib/hanami/model/migrator/mysql_adapter.rb index c3a64885..91a622f2 100644 --- a/lib/hanami/model/migrator/mysql_adapter.rb +++ b/lib/hanami/model/migrator/mysql_adapter.rb @@ -20,11 +20,11 @@ class MySQLAdapter < Adapter # @api private def create new_connection(global: true).run %(CREATE DATABASE `#{database}`;) - rescue Sequel::DatabaseError => e - message = if e.message.match(/database exists/) # rubocop:disable Performance/RedundantMatch + rescue Sequel::DatabaseError => exception + message = if exception.message.match(/database exists/) DB_CREATION_ERROR else - e.message + exception.message end raise MigrationError.new(message) @@ -34,11 +34,11 @@ def create # @api private def drop new_connection(global: true).run %(DROP DATABASE `#{database}`;) - rescue Sequel::DatabaseError => e - message = if e.message.match(/doesn\'t exist/) # rubocop:disable Performance/RedundantMatch + rescue Sequel::DatabaseError => exception + message = if exception.message.match(/doesn\'t exist/) "Cannot find database: #{database}" else - e.message + exception.message end raise MigrationError.new(message) diff --git a/lib/hanami/model/migrator/postgres_adapter.rb b/lib/hanami/model/migrator/postgres_adapter.rb index e5e6e0a7..9b534327 100644 --- a/lib/hanami/model/migrator/postgres_adapter.rb +++ b/lib/hanami/model/migrator/postgres_adapter.rb @@ -100,8 +100,8 @@ def call_db_command(command) Open3.popen3(*command_with_credentials(command)) do |_stdin, _stdout, stderr, wait_thr| raise MigrationError.new(modified_message(stderr.read)) unless wait_thr.value.success? # wait_thr.value is the exit status end - rescue SystemCallError => e - raise MigrationError.new(modified_message(e.message)) + rescue SystemCallError => exception + raise MigrationError.new(modified_message(exception.message)) end end diff --git a/lib/hanami/repository.rb b/lib/hanami/repository.rb index 076da87a..cd553e17 100644 --- a/lib/hanami/repository.rb +++ b/lib/hanami/repository.rb @@ -357,8 +357,8 @@ module Commands # entity.id # => nil - It doesn't mutate original entity def create(*args) super - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # Update a record @@ -386,8 +386,8 @@ def create(*args) # entity.id # => nil - It doesn't mutate original entity def update(*args) super - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # Delete a record @@ -405,8 +405,8 @@ def update(*args) # user = repository.delete(user.id) def delete(*args) super - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end end @@ -435,8 +435,8 @@ def initialize # user = repository.find(user.id) def find(id) root.by_pk(id).as(:entity).one - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # Return all the records for the relation diff --git a/spec/support/platform/matcher.rb b/spec/support/platform/matcher.rb index 026fa440..8328ab04 100644 --- a/spec/support/platform/matcher.rb +++ b/spec/support/platform/matcher.rb @@ -4,7 +4,7 @@ module Platform class Matcher class Nope < Hanami::Utils::BasicObject def or(other, &blk) - blk.nil? ? other : blk.call # rubocop:disable Performance/RedundantBlockCall + blk.nil? ? other : blk.call end # rubocop:disable Style/MethodMissingSuper