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

Constant Summary

Constants included from Memoizable

Memoizable::MEMOIZED_HASH

Instance Attribute Summary collapse

Attributes included from Memoizable

#__memoized__

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

Methods included from Memoizable

included

Instance Attribute Details

#definitionROM::Associations::Definition (readonly)



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

param :definition

#relationsROM::RelationRegistry (readonly)



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

option :relations, reader: true

#sourceROM::SQL::Relation (readonly)



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

option :source, reader: true

#targetROM::SQL::Relation::Name (readonly)



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

option :target, reader: true

Class Method Details

.new(definition, relations) ⇒ Object

Create an association object



41
42
43
44
45
46
47
48
# File 'lib/rom/associations/abstract.rb', line 41

def self.new(definition, relations)
  super(
    definition,
    relations: relations,
    source: relations[definition.source.relation],
    target: relations[definition.target.relation]
  )
end

Instance Method Details

#aliased?Boolean

Return if an association has an alias



55
56
57
# File 'lib/rom/associations/abstract.rb', line 55

def aliased?
  definition.aliased?
end

#apply_view(schema, relation) ⇒ Relation

Applies custom view to the default association view



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

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



64
65
66
# File 'lib/rom/associations/abstract.rb', line 64

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



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

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

#foreign_keySymbol

Return association foreign key name



92
93
94
# File 'lib/rom/associations/abstract.rb', line 92

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



152
153
154
# File 'lib/rom/associations/abstract.rb', line 152

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



121
122
123
# File 'lib/rom/associations/abstract.rb', line 121

def key
  as || name
end

#nameSymbol

Return association canonical name



73
74
75
# File 'lib/rom/associations/abstract.rb', line 73

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



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

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



112
113
114
# File 'lib/rom/associations/abstract.rb', line 112

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



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

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



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

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



199
200
201
# File 'lib/rom/associations/abstract.rb', line 199

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



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

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



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

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