Module: ROM::Repository::ClassInterface

Included in:
ROM::Repository
Defined in:
lib/rom/repository/class_interface.rb

Overview

Class-level APIs for repositories

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Class

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



23
24
25
26
27
28
29
# File 'lib/rom/repository/class_interface.rb', line 23

def [](name)
  fetch_or_store(name) do
    klass = Class.new(self < Repository::Root ? self : Repository::Root)
    klass.root(name)
    klass
  end
end

#commands(*names, mapper: nil, use: nil, plugins_options: EMPTY_HASH, **opts) ⇒ Array<Symbol>

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rom/repository/class_interface.rb', line 85

def commands(*names, mapper: nil, use: nil, plugins_options: EMPTY_HASH, **opts)
  if names.any? || opts.any?
    @commands = names + opts.to_a

    @commands.each do |spec|
      type, *view = Array(spec).flatten

      if view.size > 0
        define_restricted_command_method(type, view, mapper: mapper, use: use, plugins_options: plugins_options)
      else
        define_command_method(type, mapper: mapper, use: use, plugins_options: plugins_options)
      end
    end
  else
    @commands ||= []
  end
end

#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.

Inherits configured relations and commands



52
53
54
55
56
57
58
59
# File 'lib/rom/repository/class_interface.rb', line 52

def inherited(klass)
  super

  return if self === Repository

  klass.extend(Dry::Core::Cache)
  klass.commands(*commands)
end

#new(container, options = EMPTY_HASH) ⇒ Object

Initialize a new repository object

Parameters:

  • container (ROM::Container)

    Finalized rom container

  • options (Hash) (defaults to: EMPTY_HASH)

    Repository options

Options Hash (options):

  • :struct_namespace (Module)

    Custom struct namespace

  • :auto_struct (Boolean)

    Enable/Disable auto-struct mapping



40
41
42
43
44
45
46
47
# File 'lib/rom/repository/class_interface.rb', line 40

def new(container, options = EMPTY_HASH)
  unless relation_reader
    relation_reader(RelationReader.new(self, container.relations.elements.keys))
    include(relation_reader)
  end

  super
end