Class: ROM::Schema::AssociationsDSL
- Inherits:
- BasicObject
- Defined in:
- core/lib/rom/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(target, **options) ⇒ Associations::ManyToOne
Shortcut for many_to_one which sets alias automatically.
-
#call ⇒ AssociationSet
private
Return an association set for a schema.
-
#has_one(target, **options) ⇒ Associations::OneToOne
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::ManyToMany
Establish a many-to-many association.
-
#many_to_one(target, **options) ⇒ Associations::ManyToOne
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.
24 25 26 27 28 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 24 def initialize(source, &block) @source = source @registry = {} instance_exec(&block) end |
Instance Attribute Details
#registry ⇒ Object (readonly)
21 22 23 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 21 def registry @registry end |
#source ⇒ Object (readonly)
17 18 19 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 17 def source @source end |
Instance Method Details
#belongs_to(target, **options) ⇒ Associations::ManyToOne
Shortcut for many_to_one which sets alias automatically
154 155 156 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 154 def belongs_to(target, **) many_to_one(dataset_name(target), as: target, **) 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
180 181 182 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 180 def call AssociationSet[source.relation].build(registry) end |
#has_one(target, **options) ⇒ Associations::OneToOne
Shortcut for one_to_one which sets alias automatically
171 172 173 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 171 def has_one(target, **) one_to_one(dataset_name(target), as: target, **) end |
#many_to_many(target, **options) ⇒ Associations::ManyToMany
Establish a many-to-many association
120 121 122 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 120 def many_to_many(target, **) add(::ROM::Associations::Definitions::ManyToMany.new(source, target, **)) end |
#many_to_one(target, **options) ⇒ Associations::ManyToOne
Establish a many-to-one association
137 138 139 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 137 def many_to_one(target, **) add(::ROM::Associations::Definitions::ManyToOne.new(source, target, **)) end |
#one_to_many(target, **options) ⇒ Associations::OneToMany Also known as: has_many
Establish a one-to-many association
62 63 64 65 66 67 68 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 62 def one_to_many(target, **) if [:through] many_to_many(target, **) else add(::ROM::Associations::Definitions::OneToMany.new(source, target, **)) end end |
#one_to_one(target, **options) ⇒ Associations::OneToOne
Establish a one-to-one association
87 88 89 90 91 92 93 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 87 def one_to_one(target, **) if [:through] one_to_one_through(target, **) else add(::ROM::Associations::Definitions::OneToOne.new(source, target, **)) end end |
#one_to_one_through(target, **options) ⇒ Associations::OneToOneThrough
Establish a one-to-one association with a :through option
103 104 105 |
# File 'core/lib/rom/schema/associations_dsl.rb', line 103 def one_to_one_through(target, **) add(::ROM::Associations::Definitions::OneToOneThrough.new(source, target, **)) end |