Class: ROM::Schema

Inherits:
Object
  • Object
show all
Extended by:
Initializer, Notifications::Listener
Includes:
Enumerable, Memoizable
Defined in:
core/lib/rom/schema.rb,
core/lib/rom/schema/dsl.rb,
core/lib/rom/schema/inferrer.rb,
core/lib/rom/schema/associations_dsl.rb

Overview

Relation schema

Schemas hold detailed information about relation tuples, including their primitive types (String, Integer, Hash, etc. or custom classes), as well as various meta information like primary/foreign key and literally any other information that a given database adapter may need.

Adapters can extend this class and it can be used in adapter-specific relations. In example rom-sql extends schema with Association DSL and many additional SQL-specific APIs in schema types.

Schemas are used for projecting canonical relations into other relations and every relation object maintains its schema. This means that we always have all information about relation tuples, even when a relation was projected and diverged from its canonical form.

Furthermore schema attributes know their source relations, which makes it possible to merge schemas from multiple relations and maintain information about the source relations. In example when two relations are joined, their schemas are merged, and we know which attributes belong to which relation.

Direct Known Subclasses

Memory::Schema

Defined Under Namespace

Classes: AssociationsDSL, DSL, Inferrer

Constant Summary collapse

EMPTY_ASSOCIATION_SET =
AssociationSet.build(EMPTY_HASH).freeze
DEFAULT_INFERRER =
Inferrer.new(enabled: false).freeze
HASH_SCHEMA =
Types::Coercible::Hash
.schema(EMPTY_HASH)
.with_type_transform(type_transformation)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Schema

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 Schema.

Yields:

  • (_self)

Yield Parameters:

  • _self (ROM::Schema)

    the object that the method was called on



163
164
165
166
167
# File 'core/lib/rom/schema.rb', line 163

def initialize(*)
  super

  yield(self) if block_given?
end

Instance Attribute Details

#__memoized__Object (readonly) Originally defined in module Memoizable

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.

#associationsAssociationSet (readonly)

Returns Optional association set (this is adapter-specific).

Returns:

  • (AssociationSet)

    Optional association set (this is adapter-specific)



93
# File 'core/lib/rom/schema.rb', line 93

option :associations, default: -> { EMPTY_ASSOCIATION_SET }

#attributesArray (readonly)

Returns Array with schema attributes.

Returns:

  • (Array)

    Array with schema attributes



89
# File 'core/lib/rom/schema.rb', line 89

option :attributes, default: -> { EMPTY_ARRAY }

#canonicalSymbol (readonly)

Returns The canonical schema which is carried in all schema instances.

Returns:

  • (Symbol)

    The canonical schema which is carried in all schema instances



104
# File 'core/lib/rom/schema.rb', line 104

option :canonical, default: -> { self }

#inferrer#call (readonly)

Returns An optional inferrer object used in finalize!.

Returns:

  • (#call)

    An optional inferrer object used in finalize!



97
# File 'core/lib/rom/schema.rb', line 97

option :inferrer, default: -> { DEFAULT_INFERRER }

#nameSymbol (readonly)

Returns The name of this schema.

Returns:

  • (Symbol)

    The name of this schema



85
# File 'core/lib/rom/schema.rb', line 85

param :name

#primary_key_nameSymbol (readonly)

Returns The name of the primary key. This is set because in most of the cases relations don't have composite pks.

Returns:

  • (Symbol)

    The name of the primary key. This is set because in most of the cases relations don't have composite pks



112
# File 'core/lib/rom/schema.rb', line 112

option :primary_key_name, optional: true

#primary_key_namesArray<Symbol> (readonly)

Returns A list of all pk names.

Returns:

  • (Array<Symbol>)

    A list of all pk names



116
# File 'core/lib/rom/schema.rb', line 116

option :primary_key_names, optional: true

Class Method Details

.attributes(attributes, attr_class) ⇒ 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.



156
157
158
159
160
# File 'core/lib/rom/schema.rb', line 156

def self.attributes(attributes, attr_class)
  attributes.map do |attr|
    attr_class.new(attr[:type], **attr.fetch(:options))
  end
end

.build_attribute_info(type, **options) ⇒ Hash

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.

Builds a representation of the information needed to create an attribute.

This representation is consumed by Schema.define in order to create the actual attributes.

Returns:

  • (Hash)

    A hash with :type and :options keys.



148
149
150
151
152
153
# File 'core/lib/rom/schema.rb', line 148

def self.build_attribute_info(type, **options)
  {
    type: type,
    options: options
  }
end

.define(name, attributes: EMPTY_ARRAY, attr_class: Attribute, **options) ⇒ Schema

Define a relation schema from plain rom types and optional options

Resulting schema will decorate plain rom types with adapter-specific types By default Attribute will be used

Parameters:

  • name (Relation::Name, Symbol)

    The schema name, typically ROM::Relation::Name

Returns:



130
131
132
133
134
135
136
137
# File 'core/lib/rom/schema.rb', line 130

def self.define(name, attributes: EMPTY_ARRAY, attr_class: Attribute, **options)
  new(
    name,
    attr_class: attr_class,
    attributes: attributes(attributes, attr_class),
    **options
  ) { |schema| yield(schema) if block_given? }
end

.subscribe(event_id, query = EMPTY_HASH, &block) ⇒ Object Originally defined in module Notifications::Listener

Subscribe to events

Parameters:

  • event_id (String)

    The event key

  • query (Hash) (defaults to: EMPTY_HASH)

    An optional event filter

Returns:

  • (Object)

    self

Instance Method Details

#[](key, src = name.to_sym) ⇒ Object

Return attribute

Parameters:

  • key (Symbol)

    The attribute name

  • src (Symbol, Relation::Name) (defaults to: name.to_sym)

    The source relation (for merged schemas)

Raises:

  • KeyError



222
223
224
225
226
227
228
229
230
231
232
233
# File 'core/lib/rom/schema.rb', line 222

def [](key, src = name.to_sym)
  attr =
    if count_index[key].equal?(1)
      name_index[key]
    else
      source_index[src][key]
    end

  raise(KeyError, "#{key.inspect} attribute doesn't exist in #{src} schema") unless attr

  attr
end

#append(*new_attributes) ⇒ Schema

Append more attributes to the schema

This returns a new schema instance

Parameters:

Returns:



337
338
339
# File 'core/lib/rom/schema.rb', line 337

def append(*new_attributes)
  new(attributes + new_attributes)
end

#call(relation) ⇒ Relation

Abstract method for creating a new relation based on schema definition

This can be used by views to generate a new relation automatically. In example a schema can project a relation, join any additional relations if it includes attributes from other relations etc.

Default implementation is a no-op and it simply returns back untouched relation

Parameters:

Returns:



183
184
185
# File 'core/lib/rom/schema.rb', line 183

def call(relation)
  relation
end

#canonical?Boolean

Return if a schema is canonical

Returns:

  • (Boolean)


370
371
372
# File 'core/lib/rom/schema.rb', line 370

def canonical?
  equal?(canonical)
end

#each {|Attribute| ... } ⇒ Object

Iterate over schema's attributes

Yields:



192
193
194
# File 'core/lib/rom/schema.rb', line 192

def each(&block)
  attributes.each(&block)
end

#empty?TrueClass, FalseClass

Check if schema has any attributes

Returns:

  • (TrueClass, FalseClass)


201
202
203
# File 'core/lib/rom/schema.rb', line 201

def empty?
  attributes.empty?
end

#exclude(*names) ⇒ Schema

Exclude provided attributes from a schema

Parameters:

  • names (*Array)

    Attribute names

Returns:



253
254
255
# File 'core/lib/rom/schema.rb', line 253

def exclude(*names)
  project(*(map(&:name) - names))
end

#finalize!(**_opts) ⇒ self

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.

Finalize a schema

Returns:

  • (self)


379
380
381
382
383
# File 'core/lib/rom/schema.rb', line 379

def finalize!(**_opts)
  return self if frozen?

  freeze
end

#finalize_associations!(relations:) ⇒ self

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.

Finalize associations defined in a schema

Parameters:

Returns:

  • (self)


410
411
412
413
# File 'core/lib/rom/schema.rb', line 410

def finalize_associations!(relations:)
  set!(:associations, yield) if associations.any?
  self
end

#finalize_attributes!(gateway: nil, relations: nil) ⇒ self

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.

This hook is called when relation is being build during container finalization

When block is provided it'll be called just before freezing the instance so that additional ivars can be set

Returns:

  • (self)


393
394
395
396
397
398
399
400
401
# File 'core/lib/rom/schema.rb', line 393

def finalize_attributes!(gateway: nil, relations: nil)
  inferrer.(self, gateway).each { |key, value| set!(key, value) }

  yield if block_given?

  initialize_primary_key_names

  self
end

#foreign_key(relation) ⇒ Attribute

Return FK attribute for a given relation name

Returns:



303
304
305
# File 'core/lib/rom/schema.rb', line 303

def foreign_key(relation)
  detect { |attr| attr.foreign_key? && attr.target == relation }
end

#key?(name) ⇒ Boolean

Return if a schema includes an attribute with the given name

Parameters:

  • name (Symbol)

    The name of the attribute

Returns:

  • (Boolean)


361
362
363
# File 'core/lib/rom/schema.rb', line 361

def key?(name)
  !attributes.detect { |attr| attr.name == name }.nil?
end

#merge(other) ⇒ Schema Also known as: +

Merge with another schema

Parameters:

  • other (Schema)

    Other schema

Returns:



323
324
325
# File 'core/lib/rom/schema.rb', line 323

def merge(other)
  append(*other)
end

#prefix(prefix) ⇒ Schema

Project a schema with renamed attributes using provided prefix

Parameters:

  • prefix (Symbol)

    The name of the prefix

Returns:



280
281
282
# File 'core/lib/rom/schema.rb', line 280

def prefix(prefix)
  new(map { |attr| attr.prefixed(prefix) })
end

#primary_keyArray<Attribute>

Return primary key attributes

Returns:



312
313
314
# File 'core/lib/rom/schema.rb', line 312

def primary_key
  select(&:primary_key?)
end

#project(*names) ⇒ Schema

Project a schema to include only specified attributes

Parameters:

  • names (*Array<Symbol, Attribute>)

    Attribute names

Returns:



242
243
244
# File 'core/lib/rom/schema.rb', line 242

def project(*names)
  new(names.map { |name| name.is_a?(Symbol) ? self[name] : name })
end

#rename(mapping) ⇒ Schema

Project a schema with renamed attributes

Parameters:

  • mapping (Hash)

    The attribute mappings

Returns:



264
265
266
267
268
269
270
271
# File 'core/lib/rom/schema.rb', line 264

def rename(mapping)
  new_attributes = map do |attr|
    alias_name = mapping[attr.name]
    alias_name ? attr.aliased(alias_name) : attr
  end

  new(new_attributes)
end

#set!(key, value) ⇒ 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.



452
453
454
455
# File 'core/lib/rom/schema.rb', line 452

def set!(key, value)
  instance_variable_set("@#{key}", value)
  options[key] = value
end

#to_aryArray

Returns Array with schema attributes.

Returns:

  • (Array)

    Array with schema attributes



118
# File 'core/lib/rom/schema.rb', line 118

option :attributes, default: -> { EMPTY_ARRAY }

#to_astArray

Return AST for the schema

Returns:

  • (Array)


447
448
449
# File 'core/lib/rom/schema.rb', line 447

def to_ast
  [:schema, [name, attributes.map(&:to_ast)]]
end

#to_hHash

Coerce schema into a Attribute> Hash

Returns:

  • (Hash)


210
211
212
# File 'core/lib/rom/schema.rb', line 210

def to_h
  each_with_object({}) { |attr, h| h[attr.name] = attr }
end

#to_input_hashDry::Types::Hash

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.

Return coercion function using attribute types

This is used for input_schema in relations, typically commands use it for processing input

Returns:

  • (Dry::Types::Hash)


436
437
438
439
440
# File 'core/lib/rom/schema.rb', line 436

def to_input_hash
  HASH_SCHEMA.schema(
    map { |attr| [attr.name, attr.to_write_type] }.to_h
  )
end

#to_output_hashDry::Types::Hash

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.

Return coercion function using attribute read types

This is used for output_schema in relations

Returns:

  • (Dry::Types::Hash)


422
423
424
425
426
# File 'core/lib/rom/schema.rb', line 422

def to_output_hash
  HASH_SCHEMA.schema(
    map { |attr| [attr.key, attr.to_read_type] }.to_h
  )
end

#uniq(&block) ⇒ Schema

Return a new schema with uniq attributes

Returns:



346
347
348
349
350
351
352
# File 'core/lib/rom/schema.rb', line 346

def uniq(&block)
  if block
    new(attributes.uniq(&block))
  else
    new(attributes.uniq(&:name))
  end
end

#wrap(prefix = name.dataset) ⇒ Schema

Return new schema with all attributes marked as prefixed and wrapped

This is useful when relations are joined and the right side should be marked as wrapped

Parameters:

  • prefix (Symbol) (defaults to: name.dataset)

    The prefix used for aliasing wrapped attributes

Returns:



294
295
296
# File 'core/lib/rom/schema.rb', line 294

def wrap(prefix = name.dataset)
  new(map { |attr| attr.wrapped? ? attr : attr.wrapped(prefix) })
end