Class: ROM::Environment

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

Overview

The environment configures repositories and loads schema with relations

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(repositories, registry = {}) ⇒ 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.

Build a new environment

Parameters:

  • repositories (Hash)
  • registry (Hash) (defaults to: {})

    for relations

Returns:



43
44
45
# File 'lib/rom/environment.rb', line 43

def self.build(repositories, registry = {})
  new(repositories, registry)
end

.setup(config) ⇒ Environment

Build an environment instance from a repository config hash

Examples:


config = { 'test' => 'memory://test' }
env    = ROM::Environment.setup(config)

Parameters:

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

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

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/rom/environment.rb', line 24

def self.setup(config)
  return config if config.kind_of?(self)

  repositories = config.each_with_object({}) { |(name, uri), hash|
    hash[name.to_sym] = Repository.build(name, Addressable::URI.parse(uri))
  }

  build(repositories)
end

Instance Method Details

#[](name) ⇒ Relation

Return registered relation

Examples:


env[:users]

Parameters:

  • relation (Symbol)

    name

Returns:



110
111
112
# File 'lib/rom/environment.rb', line 110

def [](name)
  registry[name]
end

#[]=(name, relation) ⇒ 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.

Register a rom relation

Returns:



119
120
121
# File 'lib/rom/environment.rb', line 119

def []=(name, relation)
  registry[name] = relation
end

#mapping(&block) ⇒ Mapping

Define mapping for relations

Examples:


env.schema do
  base_relation :users do
    repository :test

    attribute :id,        Integer
    attribtue :user_name, String
  end
end

env.mapping do
  users do
    model User

    map :id
    map :user_name, :to => :name
  end
end

Returns:



95
96
97
# File 'lib/rom/environment.rb', line 95

def mapping(&block)
  Mapping.build(self, schema, &block)
end

#repository(name) ⇒ Repository

The repository with the given name

Returns:



128
129
130
# File 'lib/rom/environment.rb', line 128

def repository(name)
  repositories[name]
end

#schema(&block) ⇒ Schema

Build a relation schema for this environment

Examples:

env = Environment.coerce(test: 'memory://test')

env.schema do
  base_relation :users do
    repository :test

    attribute :id, Integer
    attribute :name, String
  end
end

Returns:



64
65
66
67
68
# File 'lib/rom/environment.rb', line 64

def schema(&block)
  @schema ||= Schema.build(repositories)
  @schema.call(&block) if block
  @schema
end