lib/hanami/repository.rb in hanami-model-1.1.0 vs lib/hanami/repository.rb in hanami-model-1.2.0.beta1

- old
+ new

@@ -130,10 +130,37 @@ # @api private def self.container Hanami::Model.container end + # Define a new ROM::Command while preserving the defaults used by Hanami itself. + # + # It allows the user to define a new command to, for example, + # create many records at the same time and still get entities back. + # + # The first argument is the command and relation it will operate on. + # + # @return [ROM::Command] the created command + # + # @example + # # In this example, calling the create_many method with and array of data, + # # would result in the creation of records and return an Array of Task entities. + # + # class TaskRepository < Hanami::Repository + # def create_many(data) + # command(create: :tasks, result: :many).call(data) + # end + # end + # + # @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. # # It auto-infers the underlying database table. # @@ -160,10 +187,10 @@ root(relation) class_eval %{ 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.