Class: ROM::Repository::Root

Inherits:
ROM::Repository show all
Extended by:
ClassMacros
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")

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

user_repo.delete(user.id)

Constant Summary

Constants inherited from ROM::Repository

VERSION

Instance Attribute Summary collapse

Attributes inherited from ROM::Repository

#container, #mappers, #relations

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ROM::Repository

#command

Methods included from ClassInterface

#[], #commands, #inherited, #relations

Constructor Details

#initialize(container) ⇒ Root

Returns a new instance of Root.



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

def initialize(container)
  super
  @root = relations[self.class.root]
end

Instance Attribute Details

#rootObject (readonly)



36
37
38
# File 'lib/rom/repository/root.rb', line 36

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



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

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

Instance Method Details

#aggregate(*associations) ⇒ RelationProxy #aggregate(options) ⇒ RelationProxy

Compose a relation aggregate from the root relation

Overloads:

  • #aggregate(*associations) ⇒ RelationProxy

    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(options) ⇒ RelationProxy

    Composes an aggregate by delegating to combine_children method.

    Parameters:

    • options (Hash)

      An option hash

    See Also:

    • RelationProxy::Combine#combine_children

Returns:



72
73
74
75
76
77
78
# File 'lib/rom/repository/root.rb', line 72

def aggregate(*args)
  if args[0].is_a?(Hash) && args.size == 1
    root.combine_children(args[0])
  else
    root.combine(*args)
  end
end

#changeset(name, *args) ⇒ Object #changeset(data) ⇒ Changeset::Create #changeset(restriction_arg, data) ⇒ Changeset::Update

Overloads:

  • #changeset(name, *args) ⇒ Object

    Delegate to Repository#changeset

  • #changeset(data) ⇒ Changeset::Create

    Builds a create changeset for the root relation

    Examples:

    user_repo.changeset(name: "Jane")

    Parameters:

    • data (Hash)

      New data

    Returns:

  • #changeset(restriction_arg, data) ⇒ Changeset::Update

    Builds an update changeset for the root relation

    Examples:

    user_repo.changeset(1, name: "Jane Doe")

    Parameters:

    • restriction_arg (Object)

      An argument for the restriction view

    Returns:



101
102
103
104
105
106
107
# File 'lib/rom/repository/root.rb', line 101

def changeset(*args)
  if args.first.is_a?(Symbol) && relations.key?(args.first)
    super
  else
    super(root.name, *args)
  end
end