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.
23 24 25 26 27 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 23 def initialize(source, &block) @source = source @registry = {} instance_exec(&block) end |
Instance Attribute Details
#registry ⇒ Object (readonly)
20 21 22 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 20 def registry @registry end |
#source ⇒ Object (readonly)
16 17 18 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 16 def source @source end |
Instance Method Details
#belongs_to(name, options = {}) ⇒ Associations::ManyToOne
Shortcut for many_to_one which sets alias automatically
144 145 146 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 144 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
170 171 172 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 170 def call AssociationSet.new(registry) end |
#has_one(name, options = {}) ⇒ Associations::ManyToOne
Shortcut for one_to_one which sets alias automatically
161 162 163 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 161 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
110 111 112 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 110 def many_to_many(target, = {}) add(::ROM::Associations::Definitions::ManyToMany.new(source, target, )) end |
#many_to_one(target, options = {}) ⇒ Associations::OneToOne
Establish a many-to-one association
127 128 129 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 127 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
52 53 54 55 56 57 58 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 52 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
77 78 79 80 81 82 83 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 77 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
93 94 95 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 93 def one_to_one_through(target, = {}) add(::ROM::Associations::Definitions::OneToOneThrough.new(source, target, )) end |