Sha256: f4e58a9b4203f11651849a04a096e0ad558f9947d328d1c9d24b378619d2a1e1

Contents?: true

Size: 738 Bytes

Versions: 11

Compression:

Stored size: 738 Bytes

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
joiner-0.6.0 lib/joiner/path.rb
joiner-0.5.0 lib/joiner/path.rb
joiner-0.4.2 lib/joiner/path.rb
joiner-0.4.1 lib/joiner/path.rb
joiner-0.4.0 lib/joiner/path.rb
joiner-0.3.4 lib/joiner/path.rb
joiner-0.3.3 lib/joiner/path.rb
joiner-0.3.2 lib/joiner/path.rb
joiner-0.3.1 lib/joiner/path.rb
joiner-0.3.0 lib/joiner/path.rb
joiner-0.2.0 lib/joiner/path.rb