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
- #registry ⇒ Object readonly
- #source ⇒ Object readonly
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ Associations::ManyToOne
Shortcut for many_to_one which sets alias automatically.
-
#call ⇒ AssociationSet
private
Return an association set for a schema.
-
#has_one(name, options = {}) ⇒ Associations::ManyToOne
Shortcut for one_to_one which sets alias automatically.
-
#initialize(source, &block) ⇒ AssociationsDSL
constructor
private
A new instance of AssociationsDSL.
-
#many_to_many(target, options = {}) ⇒ Associations::OneToOne
Establish a many-to-many association.
-
#many_to_one(target, options = {}) ⇒ Associations::OneToOne
Establish a many-to-one association.
-
#one_to_many(target, options = {}) ⇒ Associations::OneToMany
(also: #has_many)
Establish a one-to-many association.
-
#one_to_one(target, options = {}) ⇒ Associations::OneToOne
Establish a one-to-one association.
-
#one_to_one_through(target, options = {}) ⇒ Associations::OneToOneThrough
Establish a one-to-one association with a :through option.
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
143 144 145 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 143 def belongs_to(name, = {}) many_to_one(dataset_name(name), {as: name}.merge()) 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
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
160 161 162 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 160 def has_one(name, = {}) one_to_one(dataset_name(name), {as: name}.merge()) end |
#many_to_many(target, options = {}) ⇒ Associations::OneToOne
Establish a many-to-many association
109 110 111 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 109 def many_to_many(target, = {}) add(Association::ManyToMany.new(source, target, )) end |
#many_to_one(target, options = {}) ⇒ Associations::OneToOne
Establish a many-to-one association
126 127 128 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 126 def many_to_one(target, = {}) add(Association::ManyToOne.new(source, target, )) end |
#one_to_many(target, options = {}) ⇒ Associations::OneToMany Also known as: has_many
Establish a one-to-many association
51 52 53 54 55 56 57 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 51 def one_to_many(target, = {}) if [:through] many_to_many(target, ) else add(Association::OneToMany.new(source, target, )) end end |
#one_to_one(target, options = {}) ⇒ Associations::OneToOne
Establish a one-to-one association
76 77 78 79 80 81 82 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 76 def one_to_one(target, = {}) if [:through] one_to_one_through(target, ) else add(Association::OneToOne.new(source, target, )) end end |
#one_to_one_through(target, options = {}) ⇒ Associations::OneToOneThrough
Establish a one-to-one association with a :through option
92 93 94 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 92 def one_to_one_through(target, = {}) add(Association::OneToOneThrough.new(source, target, )) end |