class JoinByPathsHelper

Constants

EMPTY_ARRAY

Attributes

result[R]
start_model[R]

Public Class Methods

new(smodel) click to toggle source
Calls superclass method
# File lib/sequel/plugins/join_by_paths.rb, line 84
def initialize(smodel)
  super()
  @start_model = smodel
end

Public Instance Methods

add(path_str) click to toggle source
Calls superclass method
# File lib/sequel/plugins/join_by_paths.rb, line 153
def add(path_str)
  super(QPath.new(@start_model, path_str))
end
build_unique_join_segments() click to toggle source
# File lib/sequel/plugins/join_by_paths.rb, line 89
def build_unique_join_segments
  return (@result = EMPTY_ARRAY) if empty?

  maxlen = map(&:size).max
  iset = nil
  @result = []

  (0...maxlen).each do |i|
    iset = Set.new
    each { |qp| (iset << qp.segments[i]) if qp.segments[i] }
    @result << iset
  end

  @result.map! do |jseg|
    jseg.map  do |seg|
      leftm = seg.first.model_class
      assoc = leftm.association_reflection(seg.last.to_sym)

      rightm = seg.last.model_class
      # cf.  documentation in sequel/model/associations.rb
      case assoc[:type]
      # :many_to_one :: Foreign key in current model's table points to
      #                 associated model's primary key.
      when :many_to_one
        lks = [assoc[:key]].flatten
        rks = [rightm.primary_key].flatten

      when :one_to_many, :one_to_one
        # :one_to_many :: Foreign key in associated model's table points to this
        #                 model's primary key.
        # :one_to_one :: Similar to one_to_many in terms of foreign keys, but
        #                only one object is associated to the current object through the
        #                association.
        lks = [leftm.primary_key].flatten
        rks = [assoc[:key]].flatten

        # TODO
        # when         # :many_to_many :: A join table is used that has a foreign key that points
        #                  to this model's primary key and a foreign key that points to the
        #                  associated model's primary key.  Each current model object can be
        #                  associated with many associated model objects, and each associated
        #                  model object can be associated with many current model objects.
        # when    # :one_through_one :: Similar to many_to_many in terms of foreign keys, but only one object
        #                     is associated to the current object through the association.
        #                     Provides only getter methods, no setter or modification methods.

      end

      lks.map! { |k| Sequel[seg.first.alias_sym][k] } unless seg.first.empty?

      rks.map! { |k| Sequel[seg.last.alias_sym][k] }

      { type: assoc[:type],
        segment: seg,
        left: leftm.table_name,
        right: rightm.table_name,
        alias: seg.last.alias_sym,
        left_keys: lks,
        right_keys: rks,
        cond: rks.zip(lks).to_h }
    end
  end
end
dataset(start_dtset = nil) click to toggle source
# File lib/sequel/plugins/join_by_paths.rb, line 157
def dataset(start_dtset = nil)
  start_dataset = (start_dtset || @start_model.dataset)
  return start_dataset if empty?

  build_unique_join_segments
  @result.flatten.inject(start_dataset) do |dt, jo|
    dt.left_join(Sequel[jo[:right]].as(jo[:alias]),
                 jo[:cond],
                 implicit_qualifier: jo[:left])
  end
end
join_by_paths_helper(*pathlist) click to toggle source
# File lib/sequel/plugins/join_by_paths.rb, line 169
def join_by_paths_helper(*pathlist)
  pathlist.each { |path_str| add path_str }
  self
end