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
30
31
32
33
34
35
36
# File 'lib/rom/repository/class_interface.rb', line 23

def [](name)
  fetch_or_store(name) do
    base =
      if self < Repository::Root # rubocop:disable Style/MinMaxComparison
        self
      else
        Repository::Root
      end

    klass = ::Class.new(base)
    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

rubocop:disable Metrics/MethodLength

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rom/repository/class_interface.rb', line 111

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.empty?
        define_command_method(
          type,
          mapper: mapper,
          use: use,
          plugins_options: plugins_options
        )
      else
        define_restricted_command_method(
          type,
          view,
          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



76
77
78
79
80
81
82
83
# File 'lib/rom/repository/class_interface.rb', line 76

def inherited(klass)
  super

  return if self === Repository # rubocop:disable Style/CaseEquality

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

#new(container, **options) ⇒ Object #new(**options) ⇒ Object

Initialize a new repository object, establishing configured relation proxies from the passed container

Overloads:

  • #new(container, **options) ⇒ Object

    Initialize with container as leading parameter

    Parameters:

    • container (ROM::Container)

      Finalized rom container

    • options (Hash)

      Repository options

    Options Hash (**options):

    • :struct_namespace (Module)

      Custom struct namespace

    • :auto_struct (Boolean)

      Enable/Disable auto-struct mapping

  • #new(**options) ⇒ Object

    Inititalize with container as option

    Parameters:

    • options (Hash)

      Repository options

    Options Hash (**options):

    • :container (ROM::Container)

      Finalized rom container

    • :struct_namespace (Module)

      Custom struct namespace

    • :auto_struct (Boolean)

      Enable/Disable auto-struct mapping



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rom/repository/class_interface.rb', line 59

def new(container = nil, root: Undefined, **options)
  container ||= options.fetch(:container)

  unless self < relation_reader
    include relation_reader.new(
      relations: container.relations.elements.keys,
      cache: container.cache,
      root: root
    )
  end

  super(**options, container: container)
end

#use(plugin, **options) ⇒ Object



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

def use(plugin, **options)
  ROM.plugin_registry[:repository].fetch(plugin).apply_to(self, **options)
end