Class: ROM::Repository::Root

Inherits:
ROM::Repository show all
Defined in:
lib/rom/repository/root.rb

Overview

A specialized repository type dedicated to work with a root relation

This repository type builds commands and aggregates for its root relation

Examples:

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

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

user_repo = UserRepo.new(rom)

user = user_repo.create(name: "Jane")

user_repo.update(user.id, name: "Jane Doe")

user_repo.delete(user.id)

Constant Summary

Constants inherited from ROM::Repository

VERSION

Instance Attribute Summary collapse

Attributes inherited from ROM::Repository

#auto_struct, #container, #relations, #struct_namespace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ROM::Repository

auto_struct, #inspect, relation_reader, #transaction

Methods included from ClassInterface

#[], #commands, #inherited, #new

Constructor Details

#initialize(container, options = EMPTY_HASH) ⇒ Root

Returns a new instance of Root.



59
60
61
62
# File 'lib/rom/repository/root.rb', line 59

def initialize(container, options = EMPTY_HASH)
  super
  @root = set_relation(self.class.root)
end

Instance Attribute Details

#rootObject (readonly)



48
49
50
# File 'lib/rom/repository/root.rb', line 48

def root
  @root
end

Class Method Details

.inherited(klass) ⇒ Object

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.

Sets descendant root relation



53
54
55
56
# File 'lib/rom/repository/root.rb', line 53

def self.inherited(klass)
  super
  klass.root(root)
end

.rootSymbol .root(identifier) ⇒ Symbol

Get or set repository root relation identifier

This method is automatically used when you define a class using MyRepo shortcut

Overloads:

  • .rootSymbol

    Return root relation identifier

    Returns:

    • (Symbol)
  • .root(identifier) ⇒ Symbol

    Set root relation identifier

    Returns:

    • (Symbol)


44
# File 'lib/rom/repository/root.rb', line 44

defines :root

Instance Method Details

#aggregate(*associations) ⇒ Relation #aggregate(*associations, *assoc_opts) ⇒ Relation #aggregate(options) ⇒ Relation

Compose a relation aggregate from the root relation

Overloads:

  • #aggregate(*associations) ⇒ Relation

    Composes an aggregate from configured associations on the root relation

    Examples:

    user_repo.aggregate(:tasks, :posts)

    Parameters:

    • *associations (Array<Symbol>)

      A list of association names

  • #aggregate(*associations, *assoc_opts) ⇒ Relation

    Composes an aggregate from configured associations and assoc opts on the root relation

    Examples:

    user_repo.aggregate(:tasks, posts: :tags)

    Parameters:

    • *associations (Array<Symbol>)

      A list of association names

    • Association (Hash)

      options for nested aggregates

  • #aggregate(options) ⇒ Relation

    Composes an aggregate by delegating to combine method.

    Examples:

    user_repo.aggregate(tasks: :labels)
    user_repo.aggregate(posts: [:tags, :comments])

    Parameters:

    • options (Hash)

      An option hash

    See Also:

    • Relation::Combine#combine_children

Returns:

  • (Relation)


98
99
100
# File 'lib/rom/repository/root.rb', line 98

def aggregate(*args)
  root.combine(*args)
end