class ROM::Container

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:

@example 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"}]

@example 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"}]

@example multi-step setup with auto-registration

config = ROM::Configuration.new(:sql, 'sqlite::memory')
config.auto_registration('./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"}]

@api public

Public Class Methods

new(gateways, relations, mappers, commands) click to toggle source

@api private

Calls superclass method
# 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

Public Instance Methods

commands() click to toggle source

Return command registry

@return [Hash<Symbol=>CommandRegistry]

@api public

# File lib/rom/container.rb, line 147
def commands
  self[:commands]
end
disconnect() click to toggle source

Disconnect all gateways

@example

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

@return [Hash<Symbol=>Gateway>] a hash with disconnected gateways

@api public

# File lib/rom/container.rb, line 161
def disconnect
  gateways.each_value(&:disconnect)
end
gateways() click to toggle source

Return registered gateways

@return [Hash<Symbol=>Gateway>]

@api public

# File lib/rom/container.rb, line 120
def gateways
  self[:gateways]
end
mappers() click to toggle source

Return mapper registry for all relations

@return [Hash<Symbol=>MapperRegistry]

@api public

# File lib/rom/container.rb, line 129
def mappers
  self[:mappers]
end
relations() click to toggle source

Return relation registry

@return [RelationRegistry]

@api public

# File lib/rom/container.rb, line 138
def relations
  self[:relations]
end