Class: ROM::Schema::AssociationsDSL
- Inherits:
- BasicObject
- Includes:
- Associations::Definitions
- Defined in:
- 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
- #inflector ⇒ Object readonly
- #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 ⇒ Array<Association::Definition]
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, inflector = Inflector, &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, inflector = Inflector, &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.
34 35 36 37 38 39 |
# File 'lib/rom/schema/associations_dsl.rb', line 34 def initialize(source, inflector = Inflector, &block) @source = source @inflector = inflector @registry = {} instance_exec(&block) if block end |
Instance Attribute Details
#inflector ⇒ Object (readonly)
27 28 29 |
# File 'lib/rom/schema/associations_dsl.rb', line 27 def inflector @inflector end |
#registry ⇒ Object (readonly)
31 32 33 |
# File 'lib/rom/schema/associations_dsl.rb', line 31 def registry @registry end |
#source ⇒ Object (readonly)
23 24 25 |
# File 'lib/rom/schema/associations_dsl.rb', line 23 def source @source end |
Instance Method Details
#belongs_to(target, **options) ⇒ Associations::ManyToOne
Shortcut for many_to_one which sets alias automatically
165 166 167 |
# File 'lib/rom/schema/associations_dsl.rb', line 165 def belongs_to(target, **) many_to_one(dataset_name(target), as: target, **) end |
#call ⇒ Array<Association::Definition]
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
191 192 193 |
# File 'lib/rom/schema/associations_dsl.rb', line 191 def call registry.values end |
#has_one(target, **options) ⇒ Associations::OneToOne
Shortcut for one_to_one which sets alias automatically
182 183 184 |
# File 'lib/rom/schema/associations_dsl.rb', line 182 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
131 132 133 |
# File 'lib/rom/schema/associations_dsl.rb', line 131 def many_to_many(target, **) add(build(ManyToMany, target, )) end |
#many_to_one(target, **options) ⇒ Associations::ManyToOne
Establish a many-to-one association
148 149 150 |
# File 'lib/rom/schema/associations_dsl.rb', line 148 def many_to_one(target, **) add(build(ManyToOne, target, )) end |
#one_to_many(target, **options) ⇒ Associations::OneToMany Also known as: has_many
Establish a one-to-many association
73 74 75 76 77 78 79 |
# File 'lib/rom/schema/associations_dsl.rb', line 73 def one_to_many(target, **) if [:through] many_to_many(target, **, inflector: inflector) else add(build(OneToMany, target, )) end end |
#one_to_one(target, **options) ⇒ Associations::OneToOne
Establish a one-to-one association
98 99 100 101 102 103 104 |
# File 'lib/rom/schema/associations_dsl.rb', line 98 def one_to_one(target, **) if [:through] one_to_one_through(target, **) else add(build(OneToOne, target, )) end end |
#one_to_one_through(target, **options) ⇒ Associations::OneToOneThrough
Establish a one-to-one association with a :through option
114 115 116 |
# File 'lib/rom/schema/associations_dsl.rb', line 114 def one_to_one_through(target, **) add(build(OneToOneThrough, target, )) end |