Module: ROM::SQL::Relation::ClassMethods

Defined in:
lib/rom/fmp/relation/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#finalize(relations, relation) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rom/fmp/relation/class_methods.rb', line 30

def finalize(relations, relation)
  model.set_dataset(relation.dataset)
  model.dataset.naked!

  associations.each do |*args, options|
    model = relation.model
    other = relations[options.fetch(:relation)].model

    model.public_send(*args, options.merge(class: other))
  end

  model.freeze

  super
end

#inherited(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/rom/fmp/relation/class_methods.rb', line 5

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



20
21
22
# File 'lib/rom/fmp/relation/class_methods.rb', line 20

def many_to_many(name, options = {})
  associations << [__method__, name, options.merge(relation: name)]
end

#many_to_one(name, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/rom/fmp/relation/class_methods.rb', line 24

def many_to_one(name, options = {})
  relation_name = Inflector.pluralize(name).to_sym
  new_options = options.merge(relation: relation_name)
  associations << [__method__, name, new_options]
end

#one_to_many(name, options) ⇒ Object



16
17
18
# File 'lib/rom/fmp/relation/class_methods.rb', line 16

def one_to_many(name, options)
  associations << [__method__, name, options.merge(relation: name)]
end