class ROM::Repository

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.

@example

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 Repository::Root

@api public

Constants

VERSION

Attributes

relations[R]

@!attribute [r] relations

@return [RelationRegistry] The relation proxy registry used by a repo

Public Class Methods

new(*) click to toggle source

Initializes a new repository object

@api private

Calls superclass method
# File lib/rom/repository.rb, line 107
def initialize(*)
  super
  @relations = {}
end

Public Instance Methods

inspect() click to toggle source

Return a string representation of a repository object

@return [String]

@api public

# File lib/rom/repository.rb, line 155
def inspect
  %(#<#{self.class} struct_namespace=#{struct_namespace} auto_struct=#{auto_struct}>)
end
session() { |session| ... } click to toggle source

Start a session for multiple changesets

TODO: this is partly done, needs tweaks in changesets so that we can gather

command results and return them in a nice way

@!visibility private

@api public

# File lib/rom/repository.rb, line 167
def session
  session = Session.new(self)
  yield(session)
  transaction { session.commit! }
end
transaction(*args, &block) click to toggle source

Open a database transaction

@example commited transaction

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

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

@example with a rollback

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

user
# nil

@example with automatic savepoints

user = transaction(auto_savepoint: true) do
  create(changeset(name: 'Jane'))

  transaction do |t|
    update(changeset(name: 'John'))
    t.rollback!
  end
end

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

@api public

# File lib/rom/repository.rb, line 146
def transaction(*args, &block)
  container.gateways[:default].transaction(*args, &block)
end