Class: ROM::SQL::Schema::AssociationsDSL
- Inherits:
- BasicObject
- Defined in:
- lib/rom/sql/schema/associations_dsl.rb
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #belongs_to(name, options = {}) ⇒ Object
- #call ⇒ Object
- #has_one(name, options = {}) ⇒ Object
-
#initialize(source, &block) ⇒ AssociationsDSL
constructor
A new instance of AssociationsDSL.
- #many_to_many(target, options = {}) ⇒ Object
- #many_to_one(target, options = {}) ⇒ Object
- #one_to_many(target, options = {}) ⇒ Object (also: #has_many)
- #one_to_one(target, options = {}) ⇒ Object
- #one_to_one_through(target, options = {}) ⇒ Object
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
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
7 8 9 |
# File 'lib/rom/sql/schema/associations_dsl.rb', line 7 def registry @registry end |
#source ⇒ Object (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, = {}) many_to_one(dataset_name(name), .merge(as: [:as] || name)) end |
#call ⇒ Object
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, = {}) one_to_one(dataset_name(name), .merge(as: [: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, = {}) add(Association::ManyToMany.new(source, target, )) 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, = {}) add(Association::ManyToOne.new(source, target, )) 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, = {}) if [:through] many_to_many(target, ) else add(Association::OneToMany.new(source, target, )) 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, = {}) if [:through] one_to_one_through(target, ) else add(Association::OneToOne.new(source, target, )) 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, = {}) add(Association::OneToOneThrough.new(source, target, )) end |