Class: ROM::Associations::Definitions::Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Initializer
Defined in:
lib/rom/associations/definitions/abstract.rb

Overview

Abstract association definition object

API:

  • public

Direct Known Subclasses

ManyToMany, ManyToOne, OneToMany, OneToOne

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

Instance Attribute Details

#aliasSymbol (readonly)

Returns An optional association alias.

Returns:

  • An optional association alias



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

option :as, Types::Strict::Symbol.optional, optional: true

#combine_keysHash<Symbol=>Symbol> (readonly)

Returns Override inferred combine keys.

Returns:

  • Override inferred combine keys



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

option :combine_keys, optional: true

#foreign_keySymbol (readonly)

Returns an optional association alias name.

Returns:

  • an optional association alias name



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

option :foreign_key, Types::Optional::Strict::Symbol, optional: true

#nameSymbol (readonly)

Returns The name of an association.

Returns:

  • The name of an association



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

option :name, Types::Strict::Symbol, default: -> { target.to_sym }

#overrideTrueClass, FalseClass (readonly)

Returns Whether custom view should override default one or not.

Returns:

  • Whether custom view should override default one or not



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

option :override, optional: true, default: -> { false }

#relationSymbol (readonly)

Returns an optional relation identifier for the target.

Returns:

  • an optional relation identifier for the target



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

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

#resultSymbol (readonly)

Returns either :one or :many.

Returns:

  • either :one or :many



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

option :result, Types::Strict::Symbol, default: -> { self.class.result }

#sourceRelation::Name (readonly)

Returns the source relation name.

Returns:

  • the source relation name



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

param :source

#targetRelation::Name (readonly)

Returns the target relation name.

Returns:

  • the target relation name



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

param :target

#viewSymbol (readonly)

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

Returns:

  • An optional view that should be used to extend assoc relation



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

option :view, optional: true

Class Method Details

.new(source, target, **opts) ⇒ Object

Instantiate a new association definition

Parameters:

  • The name of the source dataset

  • The name of the target dataset

  • The option hash

Options Hash (**opts):

  • :as (Symbol)

    The name of the association (defaults to target)

  • :relation (Symbol)

    The name of the target relation (defaults to target)

  • :foreign_key (Symbol)

    The name of a custom foreign key

  • :view (Symbol)

    The name of a custom relation view on the target’s relation side

  • :override (true, false)

    Whether provided :view should override association’s default view

API:

  • public



76
77
78
79
80
81
82
# File 'lib/rom/associations/definitions/abstract.rb', line 76

def self.new(source, target, **opts)
  source_name = Relation::Name[source]
  target_name = resolve_target_name(target, opts)
  options = process_options(target_name, opts.to_h)

  super(source_name, target_name, **options)
end

.process_options(target, options) ⇒ 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.

API:

  • private



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rom/associations/definitions/abstract.rb', line 93

def self.process_options(target, options)
  through = options[:through]

  if through
    options[:through] = ThroughIdentifier[through, target.relation, options[:assoc]]
  end

  options[:name] = target.relation

  options
end

.resolve_target_name(target, options) ⇒ 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.

API:

  • private



85
86
87
88
89
90
# File 'lib/rom/associations/definitions/abstract.rb', line 85

def self.resolve_target_name(target, options)
  dataset = target
  relation = options.fetch(:relation, target)

  Relation::Name[relation, dataset, options[:as]]
end

Instance Method Details

#aliased?Boolean

Return true if association is aliased

Returns:

API:

  • public



119
120
121
# File 'lib/rom/associations/definitions/abstract.rb', line 119

def aliased?
  options.key?(:as)
end

#override?Boolean

Return true if association’s default relation view should be overridden by a custom one

Returns:

API:

  • public



110
111
112
# File 'lib/rom/associations/definitions/abstract.rb', line 110

def override?
  options[:override].equal?(true)
end

#typeClass

Return association class for a given definition object

Returns:

API:

  • public



128
129
130
# File 'lib/rom/associations/definitions/abstract.rb', line 128

def type
  Inflector.demodulize(self.class.name).to_sym
end