Class: ROM::SQL::Schema::AssociationsDSL

Inherits:
BasicObject
Defined in:
lib/rom/sql/schema/associations_dsl.rb

Overview

Additional schema DSL for definition SQL associations

This DSL is exposed in associations do .. end blocks in schema defintions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, &block) ⇒ AssociationsDSL

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



22
23
24
25
26
# File 'lib/rom/sql/schema/associations_dsl.rb', line 22

def initialize(source, &block)
  @source = source
  @registry = {}
  instance_exec(&block)
end

Instance Attribute Details

#registry ⇒ Object (readonly)



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

def registry
  @registry
end

#source ⇒ Object (readonly)



15
16
17
# File 'lib/rom/sql/schema/associations_dsl.rb', line 15

def source
  @source
end

Instance Method Details

#belongs_to(name, options = {}) ⇒ Associations::ManyToOne

Shortcut for many_to_one which sets alias automatically

Examples:

with an alias (relation identifier is inferred via pluralization)

belongs_to :user

with an explicit alias

belongs_to :users, as: :author

Returns:

  • (Associations::ManyToOne)

See Also:



143
144
145
# File 'lib/rom/sql/schema/associations_dsl.rb', line 143

def belongs_to(name, options = {})
  many_to_one(dataset_name(name), {as: name}.merge(options))
end

#call ⇒ AssociationSet

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 an association set for a schema

Returns:

  • (AssociationSet)


169
170
171
# File 'lib/rom/sql/schema/associations_dsl.rb', line 169

def call
  AssociationSet.new(registry)
end

#has_one(name, options = {}) ⇒ Associations::ManyToOne

Shortcut for one_to_one which sets alias automatically

Examples:

with an alias (relation identifier is inferred via pluralization)

one_to_one :address

with an explicit alias and a custom view

one_to_one :posts, as: :priority_post, view: :prioritized

Returns:

  • (Associations::ManyToOne)

See Also:



160
161
162
# File 'lib/rom/sql/schema/associations_dsl.rb', line 160

def has_one(name, options = {})
  one_to_one(dataset_name(name), {as: name}.merge(options))
end

#many_to_many(target, options = {}) ⇒ Associations::OneToOne

Establish a many-to-many association

Examples:

using relation identifier

many_to_many :tasks, through: :users_tasks

Parameters:

  • target (Symbol) —

    The target relation identifier

  • options (Hash) (defaults to: {}) —

    A hash with additional options

Returns:

  • (Associations::OneToOne)

See Also:



109
110
111
# File 'lib/rom/sql/schema/associations_dsl.rb', line 109

def many_to_many(target, options = {})
  add(Association::ManyToMany.new(source, target, options))
end

#many_to_one(target, options = {}) ⇒ Associations::OneToOne

Establish a many-to-one association

Examples:

using relation identifier

many_to_one :users, as: :author

Parameters:

  • target (Symbol) —

    The target relation identifier

  • options (Hash) (defaults to: {}) —

    A hash with additional options

Returns:

  • (Associations::OneToOne)

See Also:



126
127
128
# File 'lib/rom/sql/schema/associations_dsl.rb', line 126

def many_to_one(target, options = {})
  add(Association::ManyToOne.new(source, target, options))
end

#one_to_many(target, options = {}) ⇒ Associations::OneToMany Also known as: has_many

Establish a one-to-many association

Examples:

using relation identifier

has_many :tasks

with a :through option

# this establishes many-to-many association
has_many :tasks, through: :users_tasks

using aliased association with a custom view

has_many :posts, as: :published_posts, view: :published

using custom target relation

has_many :user_posts, relation: :posts

Parameters:

  • target (Symbol) —

    The target relation identifier

  • options (Hash) (defaults to: {}) —

    A hash with additional options

Returns:

  • (Associations::OneToMany)

See Also:



51
52
53
54
55
56
57
# File 'lib/rom/sql/schema/associations_dsl.rb', line 51

def one_to_many(target, options = {})
  if options[:through]
    many_to_many(target, options)
  else
    add(Association::OneToMany.new(source, target, options))
  end
end

#one_to_one(target, options = {}) ⇒ Associations::OneToOne

Establish a one-to-one association

Examples:

using relation identifier

one_to_one :addresses, as: :address

with an intermediate join relation

one_to_one :tasks, as: :priority_task, through: :assignments

Parameters:

  • target (Symbol) —

    The target relation identifier

  • options (Hash) (defaults to: {}) —

    A hash with additional options

Returns:

  • (Associations::OneToOne)

See Also:



76
77
78
79
80
81
82
# File 'lib/rom/sql/schema/associations_dsl.rb', line 76

def one_to_one(target, options = {})
  if options[:through]
    one_to_one_through(target, options)
  else
    add(Association::OneToOne.new(source, target, options))
  end
end

#one_to_one_through(target, options = {}) ⇒ Associations::OneToOneThrough

Establish a one-to-one association with a :through option

Examples:

one_to_one_through :users, as: :author, through: :users_posts

Returns:

  • (Associations::OneToOneThrough)


92
93
94
# File 'lib/rom/sql/schema/associations_dsl.rb', line 92

def one_to_one_through(target, options = {})
  add(Association::OneToOneThrough.new(source, target, options))
end