Class: ROM::Container

Inherits:
Object
  • Object
show all
Includes:
Dry::Container::Mixin
Defined in:
lib/rom/container.rb

Overview

ROM container is an isolated environment with no global state where all components are registered. Container objects provide access to your relations, commands and mappers. ROM containers are usually configured and handled via framework integrations, although it is easy to use them standalone.

There are 3 types of container setup:

  • Setup DSL - a simple block-based configuration which allows configuring all components and gives you back a container instance. This type is suitable for small scripts, or in some cases rake tasks
  • Explicit setup - this type requires creating a configuration object, registering component classes (ie relation classes) and passing the config to container builder function. This type is suitable when your environment is not typical and you need full control over component registration
  • Explicit setup with auto-registration - same as explicit setup but allows you to configure auto-registration mechanism which will register component classes for you, based on dir/file naming conventions. This is the most common type of setup that's used by framework integrations

Examples:

in-line setup

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

  config.relation(:users) do
    schema(infer: true)

    def by_name(name)
      where(name: name)
    end
  end
end

rom.relations[:users].insert(name: "Jane")

rom.relations[:users].by_name("Jane").to_a
# [{:id=>1, :name=>"Jane"}]

multi-step setup with explicit component classes

config = ROM::Configuration.new(:sql, 'sqlite::memory')

config.default.create_table :users do
  primary_key :id
  column :name, String, null: false
end

class Users < ROM::Relation[:sql]
  schema(:users, infer: true)

  def by_name(name)
    where(name: name)
  end
end

config.register_relation(Users)

rom = ROM.container(config)

rom.relations[:users].insert(name: "Jane")

rom.relations[:users].by_name("Jane").to_a
# [{:id=>1, :name=>"Jane"}]

multi-step setup with auto-registration

config = ROM::Configuration.new(:sql, 'sqlite::memory')
config.auto_register('./persistence', namespace: false)

config.default.create_table :users do
  primary_key :id
  column :name, String, null: false
end

# ./persistence/relations/users.rb
class Users < ROM::Relation[:sql]
  schema(infer: true)

  def by_name(name)
    where(name: name)
  end
end

rom = ROM.container(config)

rom.relations[:users].insert(name: "Jane")

rom.relations[:users].by_name("Jane").to_a
# [{:id=>1, :name=>"Jane"}]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(gateways, relations, mappers, commands) ⇒ 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.


106
107
108
109
110
111
112
113
# File 'lib/rom/container.rb', line 106

def self.new(gateways, relations, mappers, commands)
  super().tap do |container|
    container.register(:gateways, gateways)
    container.register(:mappers, mappers)
    container.register(:commands, commands)
    container.register(:relations, relations)
  end
end

Instance Method Details

#commandsHash<Symbol=>CommandRegistry]

Return command registry

Returns:


147
148
149
# File 'lib/rom/container.rb', line 147

def commands
  self[:commands]
end

#disconnectHash<Symbol=>Gateway>

Disconnect all gateways

Examples:

rom = ROM.container(:sql, 'sqlite://my_db.sqlite')
rom.relations[:users].insert(name: "Jane")
rom.disconnect

Returns:

  • (Hash<Symbol=>Gateway>)

    a hash with disconnected gateways


161
162
163
# File 'lib/rom/container.rb', line 161

def disconnect
  gateways.each_value(&:disconnect)
end

#gatewaysHash<Symbol=>Gateway>

Return registered gateways

Returns:


120
121
122
# File 'lib/rom/container.rb', line 120

def gateways
  self[:gateways]
end

#mappersHash<Symbol=>MapperRegistry]

Return mapper registry for all relations

Returns:


129
130
131
# File 'lib/rom/container.rb', line 129

def mappers
  self[:mappers]
end

#relationsRelationRegistry

Return relation registry

Returns:


138
139
140
# File 'lib/rom/container.rb', line 138

def relations
  self[:relations]
end