lib/hanami/repository.rb in hanami-model-1.3.2 vs lib/hanami/repository.rb in hanami-model-1.3.3

- old
+ new

@@ -1,15 +1,17 @@ -require 'rom-repository' -require 'hanami/model/entity_name' -require 'hanami/model/relation_name' -require 'hanami/model/mapped_relation' -require 'hanami/model/associations/dsl' -require 'hanami/model/association' -require 'hanami/utils/class' -require 'hanami/utils/class_attribute' -require 'hanami/utils/io' +# frozen_string_literal: true +require "rom-repository" +require "hanami/model/entity_name" +require "hanami/model/relation_name" +require "hanami/model/mapped_relation" +require "hanami/model/associations/dsl" +require "hanami/model/association" +require "hanami/utils/class" +require "hanami/utils/class_attribute" +require "hanami/utils/io" + module Hanami # Mediates between the entities and the persistence layer, by offering an API # to query and execute commands on a database. # # @@ -105,11 +107,11 @@ # @since 0.1.0 # # @see Hanami::Entity # @see http://martinfowler.com/eaaCatalog/repository.html # @see http://en.wikipedia.org/wiki/Dependency_inversion_principle - class Repository < ROM::Repository::Root # rubocop:disable Metrics/ClassLength + class Repository < ROM::Repository::Root # Plugins for database commands # # @since 0.7.0 # @api private # @@ -153,11 +155,10 @@ # # @since 1.2.0 def command(*args, **opts, &block) opts[:use] = COMMAND_PLUGINS | Array(opts[:use]) opts[:mapper] = opts.fetch(:mapper, Model::MappedRelation.mapper_name) - super(*args, **opts, &block) end # Define a database relation, which describes how data is fetched from the # database. @@ -165,12 +166,10 @@ # It auto-infers the underlying database table. # # @since 0.7.0 # @api private # - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/AbcSize def self.define_relation a = @associations s = @schema configuration.relation(relation) do @@ -189,22 +188,18 @@ def #{relation} Hanami::Model::MappedRelation.new(@#{relation}) end }, __FILE__, __LINE__ - 4 end - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/MethodLength # Defines the mapping between a database table and an entity. # # It's also responsible to associate table columns to entity attributes. # # @since 0.7.0 # @api private # - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/AbcSize def self.define_mapping self.entity = Utils::Class.load!(entity_name) e = entity m = @mapping @@ -217,12 +212,10 @@ root = self.root configuration.mappers { define(root, &blk) } configuration.define_mappings(root, &blk) configuration.register_entity(relation, entity_name.underscore, e) end - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/MethodLength # It defines associations, by adding relations to the repository # # @since 0.7.0 # @api private @@ -300,12 +293,10 @@ end # @since 0.7.0 # @api private # - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/AbcSize def self.inherited(klass) klass.class_eval do include Utils::ClassAttribute auto_struct true @@ -330,12 +321,10 @@ prepend Commands end Hanami::Model.repositories << klass end - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/MethodLength # Extend commands from ROM::Repository with error management # # @since 0.7.0 module Commands @@ -356,12 +345,12 @@ # # user.id # => 23 # 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 # # @return [Hanami::Entity] an updated entity @@ -385,12 +374,12 @@ # # user.age # => 34 # 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 # # @return [Hanami::Entity] a deleted entity @@ -404,12 +393,12 @@ # user = repository.create(name: 'Luca') # # 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 # Initialize a new instance # @@ -434,11 +423,11 @@ # user = repository.create(name: 'Luca') # # 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 # # @return [Array<Hanami::Entity>] all the entities