Class: ROM::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/session.rb,
lib/rom/environment.rb,
lib/rom/environment/builder.rb

Overview

The environment configures repositories and loads schema with relations

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repositories, schema, relations, mappers) ⇒ Environment

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.

Returns a new instance of Environment.



28
29
30
31
32
33
# File 'lib/rom/environment.rb', line 28

def initialize(repositories, schema, relations, mappers)
  @repositories = repositories
  @schema = schema
  @relations = relations
  @mappers = mappers
end

Instance Attribute Details

#mappersHash (readonly)

Return mapper registry

Returns:

  • (Hash)


25
26
27
# File 'lib/rom/environment.rb', line 25

def mappers
  @mappers
end

#relationsObject (readonly)

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.



11
12
13
# File 'lib/rom/environment.rb', line 11

def relations
  @relations
end

#repositoriesObject (readonly)

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.



11
12
13
# File 'lib/rom/environment.rb', line 11

def repositories
  @repositories
end

#schemaSchema (readonly)

Return schema registry

Returns:



18
19
20
# File 'lib/rom/environment.rb', line 18

def schema
  @schema
end

Class Method Details

.setup(config, &block) ⇒ Environment::Builder

Setup ROM environment

Examples:


env = ROM::Environment.setup(test: 'memory://test') do
  schema do
    base_relation(:users) do
      repository :test

      attribute :id, Integer
      attribute :name, String

      key :id
    end
  end

  mapping do
    relation(:users) do
      model User

      map :id, :name
    end
  end

end

Parameters:

  • config (Environment, Hash<#to_sym, String>)

    an environment or a hash of adapter uri strings, keyed by repository name

Returns:



68
69
70
71
72
73
74
75
76
77
# File 'lib/rom/environment.rb', line 68

def self.setup(config, &block)
  builder = Builder.call(config)

  if block
    builder.instance_eval(&block)
    builder.finalize
  else
    builder
  end
end

Instance Method Details

#[](name) ⇒ Relation

Return registered relation

Examples:


env[:users]

Parameters:

  • relation (Symbol)

    name

Returns:



90
91
92
# File 'lib/rom/environment.rb', line 90

def [](name)
  relations[name]
end

#repository(name) ⇒ Repository

The repository with the given name

Returns:



99
100
101
# File 'lib/rom/environment.rb', line 99

def repository(name)
  repositories[name]
end

#session(&block) ⇒ Object

Start a new session for this environment

Examples:

env.session do |session|
  # ...
end

See Also:



31
32
33
# File 'lib/rom/session.rb', line 31

def session(&block)
  Session.start(self, &block)
end