Class: ROM::SQL::Association
- Inherits:
-
Object
- Object
- ROM::SQL::Association
- 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
Defined Under Namespace
Classes: ManyToMany, ManyToOne, Name, OneToMany, OneToOne, OneToOneThrough
Instance Attribute Summary collapse
-
#as ⇒ Symbol
(also: #name)
readonly
An optional association alias name.
-
#foreign_key ⇒ Symbol
readonly
An optional association alias name.
-
#relation ⇒ Symbol
readonly
An optional relation identifier for the target.
-
#result ⇒ Symbol
readonly
Either :one or :many.
-
#source ⇒ ROM::Relation::Name
readonly
The source relation name.
-
#target ⇒ ROM::Relation::Name
readonly
The target relation name.
-
#view ⇒ Symbol
readonly
An optional view that should be used to extend assoc relation.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_view(schema, relation) ⇒ Object
- #join(relations, type, source = relations[self.source], target = relations[self.target]) ⇒ Object
- #join_key_map(relations) ⇒ Object private
-
#qualify(name, attribute) ⇒ QualifiedAttribute
Returns a qualified attribute name for a given dataset.
- #self_ref? ⇒ Boolean
Instance Attribute Details
#as ⇒ Symbol (readonly) Also known as: name
Returns 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_key ⇒ Symbol (readonly)
Returns 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 } |
#relation ⇒ Symbol (readonly)
Returns 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 |
#result ⇒ Symbol (readonly)
Returns 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 } |
#source ⇒ ROM::Relation::Name (readonly)
Returns the source relation name.
24 |
# File 'lib/rom/sql/association.rb', line 24 param :source |
#target ⇒ ROM::Relation::Name (readonly)
Returns the target relation name.
28 |
# File 'lib/rom/sql/association.rb', line 28 param :target |
#view ⇒ Symbol (readonly)
Returns 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
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
77 78 79 |
# File 'lib/rom/sql/association.rb', line 77 def qualify(name, attribute) QualifiedAttribute[name.to_sym, attribute] end |
#self_ref? ⇒ Boolean
92 93 94 |
# File 'lib/rom/sql/association.rb', line 92 def self_ref? source.dataset == target.dataset end |