Class: ROM::SQL::Association

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Initializer
Includes:
Dry::Core::Constants
Defined in:
lib/rom/sql/association.rb,
lib/rom/sql/association/name.rb,
lib/rom/sql/association/one_to_one.rb,
lib/rom/sql/association/many_to_one.rb,
lib/rom/sql/association/one_to_many.rb,
lib/rom/sql/association/many_to_many.rb,
lib/rom/sql/association/one_to_one_through.rb

Overview

Abstract association class

Direct Known Subclasses

ManyToMany, ManyToOne, OneToMany

Defined Under Namespace

Classes: ManyToMany, ManyToOne, Name, OneToMany, OneToOne, OneToOneThrough

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#asSymbol (readonly) Also known as: name

Returns an optional association alias name.

Returns:

  • (Symbol)

    an optional association alias name



40
# File 'lib/rom/sql/association.rb', line 40

option :as, Types::Strict::Symbol, reader: true, optional: true, default: -> assoc { assoc.target.to_sym }

#foreign_keySymbol (readonly)

Returns an optional association alias name.

Returns:

  • (Symbol)

    an optional association alias name



44
# File 'lib/rom/sql/association.rb', line 44

option :foreign_key, Types::Strict::Symbol, optional: true, reader: true, default: proc { nil }

#relationSymbol (readonly)

Returns an optional relation identifier for the target.

Returns:

  • (Symbol)

    an optional relation identifier for the target



32
# File 'lib/rom/sql/association.rb', line 32

option :relation, Types::Strict::Symbol, reader: true, optional: true

#resultSymbol (readonly)

Returns either :one or :many.

Returns:

  • (Symbol)

    either :one or :many



36
# File 'lib/rom/sql/association.rb', line 36

option :result, Types::Strict::Symbol, reader: true, default: -> assoc { assoc.class.result }

#sourceROM::Relation::Name (readonly)

Returns the source relation name.

Returns:

  • (ROM::Relation::Name)

    the source relation name



24
# File 'lib/rom/sql/association.rb', line 24

param :source

#targetROM::Relation::Name (readonly)

Returns the target relation name.

Returns:

  • (ROM::Relation::Name)

    the target relation name



28
# File 'lib/rom/sql/association.rb', line 28

param :target

#viewSymbol (readonly)

Returns An optional view that should be used to extend assoc relation.

Returns:

  • (Symbol)

    An optional view that should be used to extend assoc relation



48
# File 'lib/rom/sql/association.rb', line 48

option :view, reader: true, optional: true, default: proc { nil }

Class Method Details

.new(source, target, options = EMPTY_HASH) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/rom/sql/association.rb', line 53

def self.new(source, target, options = EMPTY_HASH)
  super(
    Name[source],
    Name[options[:relation] || target, target, options[:as] || target],
    options
  )
end

Instance Method Details

#apply_view(schema, relation) ⇒ Object



82
83
84
85
# File 'lib/rom/sql/association.rb', line 82

def apply_view(schema, relation)
  view_rel = relation.public_send(view)
  schema.merge(view_rel.schema.qualified).uniq(&:to_sym).(view_rel)
end

#join(relations, type, source = relations[self.source], target = relations[self.target]) ⇒ Object



62
63
64
# File 'lib/rom/sql/association.rb', line 62

def join(relations, type, source = relations[self.source], target = relations[self.target])
  source.__send__(type, target.name.dataset, join_keys(relations)).qualified
end

#join_key_map(relations) ⇒ 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.



88
89
90
# File 'lib/rom/sql/association.rb', line 88

def join_key_map(relations)
  join_keys(relations).to_a.flatten.map(&:to_sym)
end

#qualify(name, attribute) ⇒ QualifiedAttribute

Returns a qualified attribute name for a given dataset

This is compatible with Sequel’s SQL generator and can be used in query DSL methods

Parameters:

  • name (ROM::Relation::Name)
  • attribute (Symbol)

Returns:



77
78
79
# File 'lib/rom/sql/association.rb', line 77

def qualify(name, attribute)
  QualifiedAttribute[name.to_sym, attribute]
end

#self_ref?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rom/sql/association.rb', line 92

def self_ref?
  source.dataset == target.dataset
end