Module: ROM::SQL::Relation::ClassMethods Private
- Included in:
- ROM::SQL::Relation
- Defined in:
- lib/rom/sql/relation/class_methods.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class DSL for SQL relations
Instance Method Summary collapse
-
#finalize(relations, relation) ⇒ Object
private
Finalize the relation by setting up its associations (if any).
-
#inherited(klass) ⇒ Object
private
Set up model and association ivars for descendant class.
-
#many_to_many(name, options = {}) ⇒ Object
Set up a many-to-many association.
-
#many_to_one(name, options = {}) ⇒ Object
Set up a many-to-one association.
-
#one_to_many(name, options) ⇒ Object
Set up a one-to-many association.
Instance Method Details
#finalize(relations, relation) ⇒ Object
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.
Finalize the relation by setting up its associations (if any)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rom/sql/relation/class_methods.rb', line 99 def finalize(relations, relation) return unless relation.dataset.db.table_exists?(dataset) model.set_dataset(relation.dataset) model.dataset.naked! associations.each do |*args, assoc_opts| = Hash[assoc_opts] other = relations[.delete(:relation) || args[1]].model model.public_send(*args, .merge(class: other)) end model.freeze super end |
#inherited(klass) ⇒ Object
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.
Set up model and association ivars for descendant class
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rom/sql/relation/class_methods.rb', line 11 def inherited(klass) klass.class_eval do class << self attr_reader :model, :associations end end klass.instance_variable_set('@model', Class.new(Sequel::Model)) klass.instance_variable_set('@associations', []) super end |
#many_to_many(name, options = {}) ⇒ Object
Set up a many-to-many association
67 68 69 |
# File 'lib/rom/sql/relation/class_methods.rb', line 67 def many_to_many(name, = {}) associations << [__method__, name, { relation: name }.merge()] end |
#many_to_one(name, options = {}) ⇒ Object
Set up a many-to-one association
90 91 92 93 94 |
# File 'lib/rom/sql/relation/class_methods.rb', line 90 def many_to_one(name, = {}) relation_name = Inflector.pluralize(name).to_sym = .merge(relation: relation_name) associations << [__method__, name, ] end |
#one_to_many(name, options) ⇒ Object
Set up a one-to-many association
40 41 42 |
# File 'lib/rom/sql/relation/class_methods.rb', line 40 def one_to_many(name, ) associations << [__method__, name, { relation: name }.merge()] end |