class ROM::Environment

The environment configures repositories and loads schema with relations

Public Class Methods

build(repositories, registry = {}) click to toggle source

Build a new environment

@param [Hash] repositories

@param [Hash] registry for relations

@return [Environment]

@api private

# File lib/rom/environment.rb, line 43
def self.build(repositories, registry = {})
  new(repositories, registry)
end
setup(config) click to toggle source

Build an environment instance from a repository config hash

@example

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

@param [Environment, Hash<#to_sym, String>] config

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

@return [Environment]

@api public

# 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

Public Instance Methods

[](name) click to toggle source

Return registered relation

@example

env[:users]

@param [Symbol] relation name

@return [Relation]

@api public

# File lib/rom/environment.rb, line 110
def [](name)
  registry[name]
end
[]=(name, relation) click to toggle source

Register a rom relation

@return [Environment]

@api private

# File lib/rom/environment.rb, line 119
def []=(name, relation)
  registry[name] = relation
end
mapping(&block) click to toggle source

Define mapping for relations

@example

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

@return [Mapping]

@api public

# File lib/rom/environment.rb, line 95
def mapping(&block)
  Mapping.build(self, schema, &block)
end
repository(name) click to toggle source

The repository with the given name

@return [Repository]

@api public

# File lib/rom/environment.rb, line 128
def repository(name)
  repositories[name]
end
schema(&block) click to toggle source

Build a relation schema for this environment

@example

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

env.schema do
  base_relation :users do
    repository :test

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

@return [Schema]

@api public

# File lib/rom/environment.rb, line 64
def schema(&block)
  @schema ||= Schema.build(repositories)
  @schema.call(&block) if block
  @schema
end