Class: ROM::Associations::Abstract

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Includes:
Memoizable
Defined in:
lib/rom/associations/abstract.rb

Overview

Abstract association class

Direct Known Subclasses

ManyToMany, ManyToOne, OneToMany

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

#definitionROM::Associations::Definition (readonly)

Returns Association configuration object.

Returns:

  • (ROM::Associations::Definition)

    Association configuration object



23
# File 'lib/rom/associations/abstract.rb', line 23

param :definition

#relationsROM::RelationRegistry (readonly)

Returns Relation registry.

Returns:

  • (ROM::RelationRegistry)

    Relation registry



27
# File 'lib/rom/associations/abstract.rb', line 27

option :relations, reader: true

#sourceROM::Relation (readonly)

Returns The source relation.

Returns:



31
# File 'lib/rom/associations/abstract.rb', line 31

option :source, reader: false, optional: true

#targetROM::Relation (readonly)

Returns The target relation.

Returns:



35
# File 'lib/rom/associations/abstract.rb', line 35

option :target, reader: false, optional: true

Class Method Details

.new(definition, relations) ⇒ Object

Create an association object

Parameters:

  • definition (Definition)

    The association definition object

  • relations (RelationRegistry)

    The relation registry



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

def self.new(definition, relations)
  super(definition, relations: relations)
end

Instance Method Details

#aliased?Boolean

Return if an association has an alias

Returns:

  • (Boolean)


66
67
68
# File 'lib/rom/associations/abstract.rb', line 66

def aliased?
  definition.aliased?
end

#apply_view(schema, relation) ⇒ Relation

Applies custom view to the default association view

Returns:



141
142
143
144
# File 'lib/rom/associations/abstract.rb', line 141

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

#asSymbol

Return association alias

Returns:

  • (Symbol)


75
76
77
# File 'lib/rom/associations/abstract.rb', line 75

def as
  definition.as
end

#combine_keysHash<Symbol=>Symbol>

Return combine keys hash

Combine keys are used for merging associated data together, typically these are the same as fk<=>pk mapping

Returns:

  • (Hash<Symbol=>Symbol>)


154
155
156
# File 'lib/rom/associations/abstract.rb', line 154

def combine_keys
  definition.combine_keys || {source_key => target_key}
end

#foreign_keySymbol

Return association foreign key name

Returns:

  • (Symbol)


103
104
105
# File 'lib/rom/associations/abstract.rb', line 103

def foreign_key
  definition.foreign_key
end

#join_key_mapArray<Symbol>

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 names of source PKs and target FKs

Returns:

  • (Array<Symbol>)


163
164
165
# File 'lib/rom/associations/abstract.rb', line 163

def join_key_map
  join_keys.to_a.flatten(1).map(&:key)
end

#keySymbol

Return the name of a key in tuples under which loaded association data are returned

Returns:

  • (Symbol)


132
133
134
# File 'lib/rom/associations/abstract.rb', line 132

def key
  as || name
end

#nameSymbol

Return association canonical name

Returns:

  • (Symbol)


84
85
86
# File 'lib/rom/associations/abstract.rb', line 84

def name
  definition.name
end

#nodeRelation

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 target relation configured as a combine node

Returns:



172
173
174
175
176
177
# File 'lib/rom/associations/abstract.rb', line 172

def node
  target.with(
    name: target.name.as(key),
    meta: {keys: combine_keys, combine_type: result, combine_name: key}
  )
end

#override?Boolean

Return if a custom view should override default association view

Returns:

  • (Boolean)


123
124
125
# File 'lib/rom/associations/abstract.rb', line 123

def override?
  definition.override
end

#prepare(target) ⇒ Relation

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.

Prepare association's target relation for composition

Returns:



197
198
199
200
201
202
203
# File 'lib/rom/associations/abstract.rb', line 197

def prepare(target)
  if override?
    target.public_send(view)
  else
    call(target: target)
  end
end

#resultSymbol

Return result type

This can be either :one or :many

Returns:

  • (Symbol)


114
115
116
# File 'lib/rom/associations/abstract.rb', line 114

def result
  definition.result
end

#self_ref?Boolean

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 if this association's source relation is the same as the target

Returns:

  • (Boolean)


210
211
212
# File 'lib/rom/associations/abstract.rb', line 210

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

#viewSymbol

Return the name of a custom relation view that should be use to extend or override default association view

Returns:

  • (Symbol)


94
95
96
# File 'lib/rom/associations/abstract.rb', line 94

def view
  definition.view
end

#wrapRelation

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 target relation as a wrap node

Returns:



184
185
186
187
188
189
190
# File 'lib/rom/associations/abstract.rb', line 184

def wrap
  target.with(
    name: target.name.as(key),
    schema: target.schema.wrap,
    meta: {wrap: true, combine_name: key}
  )
end