Class: ROM::Repository
- Inherits:
-
Object
- Object
- ROM::Repository
- Extended by:
- Dry::Core::ClassAttributes, Initializer, ClassInterface
- Defined in:
- lib/rom/repository.rb,
lib/rom/repository/root.rb,
lib/rom/repository/version.rb,
lib/rom/repository/command_proxy.rb,
lib/rom/repository/header_builder.rb,
lib/rom/repository/mapper_builder.rb,
lib/rom/repository/relation_proxy.rb,
lib/rom/repository/struct_builder.rb,
lib/rom/repository/class_interface.rb,
lib/rom/repository/command_compiler.rb,
lib/rom/repository/relation_proxy/wrap.rb,
lib/rom/repository/relation_proxy/combine.rb
Overview
Abstract repository class to inherit from
A repository provides access to composable relations, commands and changesets. 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.
Direct Known Subclasses
Defined Under Namespace
Modules: ClassInterface Classes: CommandCompiler, CommandProxy, HeaderBuilder, MapperBuilder, RelationProxy, Root, StructBuilder
Constant Summary collapse
- CHANGESET_TYPES =
Mapping for supported changeset classes used in #changeset(type => relation) method
{ create: Changeset::Create, update: Changeset::Update, delete: Changeset::Delete }.freeze
- VERSION =
'1.3.2'.freeze
Instance Attribute Summary collapse
-
#auto_struct ⇒ Boolean
readonly
The container used to set up a repo.
- #command_compiler ⇒ Object readonly
-
#commmand_compiler ⇒ Method
readonly
Function for compiling commands bound to a repo instance.
-
#container ⇒ ROM::Container
readonly
The container used to set up a repo.
- #mappers ⇒ Object readonly
- #relations ⇒ Object readonly
Class Method Summary collapse
-
.[](name) ⇒ Class
extended
from ClassInterface
Create a root-repository class and set its root relation.
-
.auto_struct ⇒ Object
Get or set auto_struct setting.
-
.commands(*names, mapper: nil, use: nil, **opts) ⇒ Array<Symbol>
extended
from ClassInterface
Defines command methods on a root repository.
-
.inherited(klass) ⇒ Object
extended
from ClassInterface
private
Inherits configured relations and commands.
-
.relations(*names) ⇒ Array<Symbol>
extended
from ClassInterface
Define which relations your repository is going to use.
Instance Method Summary collapse
-
#changeset(*args) ⇒ Object
Return a changeset for a relation.
-
#command(*args, **opts, &block) ⇒ ROM::Command
Return a command for a relation.
-
#initialize(container, opts = EMPTY_HASH) ⇒ Repository
constructor
Initializes a new repo by establishing configured relation proxies from the passed container.
-
#inspect ⇒ String
Return a string representation of a repository object.
-
#transaction(&block) ⇒ Object
Open a database transaction.
Constructor Details
#initialize(container, opts = EMPTY_HASH) ⇒ Repository
Initializes a new repo by establishing configured relation proxies from the passed container
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rom/repository.rb', line 111 def initialize(container, opts = EMPTY_HASH) super @mappers = MapperBuilder.new @relations = RelationRegistry.new do |registry, relations| self.class.relations.each do |name| relation = container.relation(name) proxy = RelationProxy.new( relation, name: name, mappers: mappers, registry: registry, auto_struct: auto_struct ) instance_variable_set("@#{name}", proxy) relations[name] = proxy end end @command_compiler = method(:command) end |
Instance Attribute Details
#auto_struct ⇒ Boolean (readonly)
Returns The container used to set up a repo.
91 |
# File 'lib/rom/repository.rb', line 91 option :auto_struct, default: -> { self.class.auto_struct } |
#command_compiler ⇒ Object (readonly)
103 104 105 |
# File 'lib/rom/repository.rb', line 103 def command_compiler @command_compiler end |
#commmand_compiler ⇒ Method (readonly)
Returns Function for compiling commands bound to a repo instance.
103 |
# File 'lib/rom/repository.rb', line 103 attr_reader :command_compiler |
#container ⇒ ROM::Container (readonly)
Returns The container used to set up a repo.
87 |
# File 'lib/rom/repository.rb', line 87 param :container, allow: ROM::Container |
#mappers ⇒ Object (readonly)
99 100 101 |
# File 'lib/rom/repository.rb', line 99 def mappers @mappers end |
#relations ⇒ Object (readonly)
95 96 97 |
# File 'lib/rom/repository.rb', line 95 def relations @relations end |
Class Method Details
.[](name) ⇒ Class Originally defined in module ClassInterface
Create a root-repository class and set its root relation
.auto_struct ⇒ TrueClass, FalseClass .auto_struct(value) ⇒ Class
Get or set auto_struct setting
When disabled, rom structs won’t be created
81 |
# File 'lib/rom/repository.rb', line 81 defines :auto_struct |
.commands(*names, mapper: nil, use: nil, **opts) ⇒ Array<Symbol> Originally defined in module ClassInterface
Defines command methods on a root repository
.inherited(klass) ⇒ Object Originally defined in module ClassInterface
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
.relations(*names) ⇒ Array<Symbol> Originally defined in module ClassInterface
Define which relations your repository is going to use
Instance Method Details
#changeset(name, attributes) ⇒ Changeset::Create #changeset(name, primary_key, attributes) ⇒ Changeset::Update #changeset(changeset_class) ⇒ Changeset #changeset(opts) ⇒ Changeset
Return a changeset for a relation
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rom/repository.rb', line 227 def changeset(*args) opts = { command_compiler: command_compiler } if args.size == 2 name, data = args elsif args.size == 3 name, pk, data = args elsif args.size == 1 if args[0].is_a?(Class) klass = args[0] if klass < Changeset return klass.new(relations[klass.relation], opts) else raise ArgumentError, "+#{klass.name}+ is not a Changeset subclass" end else type, relation = args[0].to_a[0] end else raise ArgumentError, 'Repository#changeset accepts 1-3 arguments' end if type klass = CHANGESET_TYPES.fetch(type) { raise ArgumentError, "+#{type.inspect}+ is not a valid changeset type. Must be one of: #{CHANGESET_TYPES.keys.inspect}" } klass.new(relation, opts) else relation = relations[name] if pk Changeset::Update.new(relation.by_pk(pk), opts.update(__data__: data)) else Changeset::Create.new(relation, opts.update(__data__: data)) end end end |
#command(type, relation) ⇒ ROM::Command #command(options) ⇒ ROM::Command #command(rel_name) ⇒ CommandRegistry #command(rel_name, &block) ⇒ ROM::Command
Return a command for a relation
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rom/repository.rb', line 170 def command(*args, **opts, &block) all_args = args + opts.to_a.flatten if all_args.size > 1 commands.fetch_or_store(all_args.hash) do compile_command(*args, **opts) end else container.command(*args, &block) end end |
#inspect ⇒ String
Return a string representation of a repository object
296 297 298 |
# File 'lib/rom/repository.rb', line 296 def inspect %(#<#{self.class} relations=[#{self.class.relations.map(&:inspect).join(' ')}]>) end |
#transaction(&block) ⇒ Object
Open a database transaction
287 288 289 |
# File 'lib/rom/repository.rb', line 287 def transaction(&block) container.gateways[:default].transaction(&block) end |