Module: ROM::SQL::Plugin::AssocMacros::ClassInterface Private

Defined in:
lib/rom/sql/plugin/assoc_macros/class_interface.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class DSL for SQL relations

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ 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.



21
22
23
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 21

def self.extended(klass)
  prepare(klass)
end

.prepare(klass) ⇒ 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.



10
11
12
13
14
15
16
17
18
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 10

def self.prepare(klass)
  klass.class_eval do
    class << self
      attr_reader :model, :associations
    end
  end
  klass.instance_variable_set('@model', Class.new(Sequel::Model))
  klass.instance_variable_set('@associations', [])
end

Instance Method Details

#finalize(relations, relation) ⇒ 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.

Finalize the relation by setting up its associations (if any)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 108

def finalize(relations, relation)
  return unless relation.dataset.db.table_exists?(dataset)

  model.set_dataset(relation.dataset)
  model.dataset.naked!

  associations.each do |*args, assoc_opts|
    options = Hash[assoc_opts]
    other = relations[options.delete(:relation) || args[1]].model
    model.public_send(*args, options.merge(class: other))
  end

  model.freeze

  super
end

#inherited(klass) ⇒ 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.

Set up model and association ivars for descendant class



28
29
30
31
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 28

def inherited(klass)
  super
  ClassInterface.prepare(klass)
end

#many_to_many(name, options = {}) ⇒ Object

Set up a many-to-many association

Examples:

class Tasks < ROM::Relation[:sql]
  many_to_many :tags,
    join_table: :task_tags,
    left_key: :task_id,
    right_key: :tag_id,

  def with_tags
    association_join(:tags)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

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

    The options hash

Options Hash (options):

  • :join_table (Symbol)

    Name of the join table

  • :left_key (Hash)

    Name of the left join key

  • :right_key (Hash)

    Name of the right join key

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



78
79
80
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 78

def many_to_many(name, options = {})
  associations << [__method__, name, { relation: name }.merge(options)]
end

#many_to_one(name, options = {}) ⇒ Object

Set up a many-to-one association

Examples:

class Tasks < ROM::Relation[:sql]
  many_to_one :users, key: :user_id

  def with_users
    association_join(:users)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

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

    The options hash

Options Hash (options):

  • :join_table (Symbol)

    Name of the join table

  • :key (Hash)

    Name of the join key

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



101
102
103
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 101

def many_to_one(name, options = {})
  associations << [__method__, name, { relation: name }.merge(options)]
end

#one_to_many(name, options) ⇒ Object

Set up a one-to-many association

Examples:

class Users < ROM::Relation[:sql]
  one_to_many :tasks, key: :user_id

  def with_tasks
    association_join(:tasks)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

  • options (Hash)

    The options hash

Options Hash (options):

  • :key (Symbol)

    Name of the key to join on

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



51
52
53
# File 'lib/rom/sql/plugin/assoc_macros/class_interface.rb', line 51

def one_to_many(name, options)
  associations << [__method__, name, { relation: name }.merge(options)]
end