File: path.rb

package info (click to toggle)
ruby-joiner 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 303; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 738 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Joiner::Path
  AGGREGATE_MACROS = [:has_many, :has_and_belongs_to_many]

  def initialize(base, path)
    @base, @path = base, path
  end

  def aggregate?
    macros.any? { |macro| AGGREGATE_MACROS.include? macro }
  end

  def macros
    reflections.collect(&:macro)
  end

  def model
    path.empty? ? base : reflections.last.try(:klass)
  end

  private

  attr_reader :base, :path

  def reflections
    klass = base
    path.collect { |reference|
      klass.reflect_on_association(reference).tap { |reflection|
        if reflection.nil?
          raise Joiner::AssociationNotFound,
            "No association matching #{base.name}, #{path.join(', ')}"
        end

        klass = reflection.klass
      }
    }
  end
end