Class: ROM::Repository

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache, Dry::Core::ClassAttributes, ClassInterface
Defined in:
repository/lib/rom/repository.rb,
repository/lib/rom/repository/root.rb,
repository/lib/rom/repository/version.rb,
repository/lib/rom/repository/class_interface.rb,
repository/lib/rom/repository/relation_reader.rb

Overview

Abstract repository class to inherit from

A repository provides access to composable relations and commands. Its job is to provide application-specific data that is already materialized, so that relations don't leak into your application layer.

Typically, you're going to work with Repository::Root that is configured to use a single relation as its root, and compose aggregates and use changesets and commands against the root relation.

Examples:

rom = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:users) do
    primary_key :id
    column :name, String
  end

  conf.default.create_table(:tasks) do
    primary_key :id
    column :user_id, Integer
    column :title, String
  end

  conf.relation(:users) do
    associations do
      has_many :tasks
    end
  end
end

class UserRepo < ROM::Repository[:users]
  def users_with_tasks
    aggregate(:tasks).to_a
  end
end

user_repo = UserRepo.new(rom)
user_repo.users_with_tasks

See Also:

Direct Known Subclasses

Root

Defined Under Namespace

Modules: ClassInterface Classes: Root

Constant Summary collapse

VERSION =
'5.2.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auto_structBoolean (readonly)

Returns The container used to set up a repo.

Returns:

  • (Boolean)

    The container used to set up a repo



101
# File 'repository/lib/rom/repository.rb', line 101

option :auto_struct, default: -> { self.class.auto_struct }

#containerROM::Container (readonly)

Returns The container used to set up a repo.

Returns:



93
# File 'repository/lib/rom/repository.rb', line 93

option :container, allow: ROM::Container

#relationsObject (readonly)



105
106
107
# File 'repository/lib/rom/repository.rb', line 105

def relations
  @relations
end

#struct_namespaceModule, Class (readonly)

Returns The namespace for auto-generated structs.

Returns:

  • (Module, Class)

    The namespace for auto-generated structs



97
# File 'repository/lib/rom/repository.rb', line 97

option :struct_namespace, default: -> { self.class.struct_namespace }

Class Method Details

.[](name) ⇒ Class Originally defined in module ClassInterface

Create a root-repository class and set its root relation

Examples:

# where :users is the relation name in your rom container
class UserRepo < ROM::Repository[:users]
end

Parameters:

  • name (Symbol)

    The relation register_as value

Returns:

  • (Class)

    descendant of ROM::Repository::Root

.auto_structObject

Get or set struct namespace



76
# File 'repository/lib/rom/repository.rb', line 76

defines :auto_struct

.commands(*names, mapper: nil, use: nil, plugins_options: EMPTY_HASH, **opts) ⇒ Array<Symbol> Originally defined in module ClassInterface

Defines command methods on a root repository

Examples:

class UserRepo < ROM::Repository[:users]
  commands :create, update: :by_pk, delete: :by_pk
end

# with custom command plugin
class UserRepo < ROM::Repository[:users]
  commands :create, use: :my_command_plugin
end

# with custom mapper
class UserRepo < ROM::Repository[:users]
  commands :create, mapper: :my_custom_mapper
end

Parameters:

  • names (Array<Symbol>)

    A list of command names

  • :mapper (Hash)

    a customizable set of options

  • :use (Hash)

    a customizable set of options

Returns:

  • (Array<Symbol>)

    A list of defined command names

#new(container, **options) ⇒ Object #new(**options) ⇒ Object Originally defined in module ClassInterface

Initialize a new repository object, establishing configured relation proxies from the passed container

Overloads:

  • #new(container, **options) ⇒ Object

    Initialize with container as leading parameter

    Parameters:

    • container (ROM::Container)

      Finalized rom container

    • options (Hash)

      Repository options

    Options Hash (**options):

    • :struct_namespace (Module)

      Custom struct namespace

    • :auto_struct (Boolean)

      Enable/Disable auto-struct mapping

  • #new(**options) ⇒ Object

    Inititalize with container as option

    Parameters:

    • options (Hash)

      Repository options

    Options Hash (**options):

    • :container (ROM::Container)

      Finalized rom container

    • :struct_namespace (Module)

      Custom struct namespace

    • :auto_struct (Boolean)

      Enable/Disable auto-struct mapping

.relation_readerRelationReader

Get or set relation reader module

Returns:

  • (RelationReader)


87
# File 'repository/lib/rom/repository.rb', line 87

defines :relation_reader

.use(plugin, **options) ⇒ Object Originally defined in module ClassInterface

Instance Method Details

#inspectString

Return a string representation of a repository object

Returns:

  • (String)


145
146
147
# File 'repository/lib/rom/repository.rb', line 145

def inspect
  %(#<#{self.class} struct_namespace=#{struct_namespace} auto_struct=#{auto_struct}>)
end

#transaction(&block) ⇒ Object

Open a database transaction

Examples:

commited transaction

user = transaction do |t|
  create(changeset(name: 'Jane'))
end

user
# => #<ROM::Struct::User id=1 name="Jane">

with a rollback

user = transaction do |t|
  changeset(name: 'Jane').commit
  t.rollback!
end

user
# nil


136
137
138
# File 'repository/lib/rom/repository.rb', line 136

def transaction(&block)
  container.gateways[:default].transaction(&block)
end