Class: ROM::Repository

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache, Dry::Core::ClassAttributes, Initializer, ClassInterface
Defined in:
lib/rom/repository.rb,
lib/rom/repository/root.rb,
lib/rom/repository/version.rb,
lib/rom/repository/class_interface.rb,
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: RelationReader, Root

Constant Summary collapse

VERSION =
'2.0.2'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassInterface

[], commands, inherited, new

Constructor Details

#initialize(container, options = EMPTY_HASH) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new repo by establishing configured relation proxies from the passed container

Parameters:

  • container (ROM::Container)

    The rom container with relations and optional commands



109
110
111
112
# File 'lib/rom/repository.rb', line 109

def initialize(container, options = EMPTY_HASH)
  super
  @relations = {}
end

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



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

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

#containerROM::Container (readonly)

Returns The container used to set up a repo.

Returns:

  • (ROM::Container)

    The container used to set up a repo



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

param :container, allow: ROM::Container

#relationsObject (readonly)



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

def relations
  @relations
end

#struct_namespaceModule, Class (readonly)

Returns The namespace for auto-generated structs.

Returns:

  • (Module, Class)

    The namespace for auto-generated structs



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

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

Class Method Details

.auto_structObject

Get or set struct namespace



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

defines :auto_struct

.relation_readerRelationReader

Get or set relation reader module

Returns:



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

defines :relation_reader

Instance Method Details

#inspectString

Return a string representation of a repository object

Returns:

  • (String)


143
144
145
# File 'lib/rom/repository.rb', line 143

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


134
135
136
# File 'lib/rom/repository.rb', line 134

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