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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, &block) ⇒ AssociationsDSL

Returns a new instance of AssociationsDSL.



9
10
11
12
13
# File 'lib/rom/sql/schema/associations_dsl.rb', line 9

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

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



7
8
9
# File 'lib/rom/sql/schema/associations_dsl.rb', line 7

def registry
  @registry
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/rom/sql/schema/associations_dsl.rb', line 7

def source
  @source
end

Instance Method Details

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



44
45
46
# File 'lib/rom/sql/schema/associations_dsl.rb', line 44

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

#callObject



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

def call
  AssociationSet.new(registry)
end

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



48
49
50
# File 'lib/rom/sql/schema/associations_dsl.rb', line 48

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

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



36
37
38
# File 'lib/rom/sql/schema/associations_dsl.rb', line 36

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

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



40
41
42
# File 'lib/rom/sql/schema/associations_dsl.rb', line 40

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

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



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

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 = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rom/sql/schema/associations_dsl.rb', line 24

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 = {}) ⇒ Object



32
33
34
# File 'lib/rom/sql/schema/associations_dsl.rb', line 32

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