Class: ROM::SQL::Association

Inherits:
Object
  • Object
show all
Extended by:
ClassMacros
Includes:
Options
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

Instance Method Summary collapse

Constructor Details

#initialize(source, target, options = EMPTY_HASH) ⇒ Association

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



40
41
42
43
44
# File 'lib/rom/sql/association.rb', line 40

def initialize(source, target, options = EMPTY_HASH)
  @source = Name[source]
  @target = Name[options[:relation] || target, target, options[:as] || target]
  super
end

Instance Attribute Details

#asSymbol (readonly) Also known as: name

Returns an optional association alias name.

Returns:

  • (Symbol)

    an optional association alias name



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

option :as, accepts: [Symbol], reader: true, default: -> assoc { assoc.target.to_sym }

#relationSymbol (readonly)

Returns an optional relation identifier for the target.

Returns:

  • (Symbol)

    an optional relation identifier for the target



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

option :relation, accepts: [Symbol], reader: true

#resultSymbol (readonly)

Returns either :one or :many.

Returns:

  • (Symbol)

    either :one or :many



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

option :result, accepts: [Symbol], reader: true, default: -> assoc { assoc.class.result }

#sourceObject (readonly)



19
20
21
# File 'lib/rom/sql/association.rb', line 19

def source
  @source
end

#targetObject (readonly)



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

def target
  @target
end

Instance Method Details

#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:



57
58
59
# File 'lib/rom/sql/association.rb', line 57

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