module Sequel::Plugins::EagerGraphEager::DatasetMethods

  1. lib/sequel/plugins/eager_graph_eager.rb

Methods

Public Instance

  1. eager_graph_eager

Protected Instance

  1. eager_graph_build_associations

Public Instance methods

eager_graph_eager(dependency_chain, *assocs)

Specify for the given dependency chain, after loading objects for the current dataset via eager_graph, eagerly load the given associations at that point in the dependency chain.

dependency_chain

Array of association symbols, with the first association symbol specifying an association in the dataset’s model, the next association specifying an association in the previous association’s associated model, and so on.

assocs

Symbols or hashes specifying associations to eagerly load at the point specified by the dependency chain.

[show source]
    # File lib/sequel/plugins/eager_graph_eager.rb
 73 def eager_graph_eager(dependency_chain, *assocs)
 74   unless dependency_chain.is_a?(Array) && dependency_chain.all?{|s| s.is_a?(Symbol)} && !dependency_chain.empty?
 75     raise Error, "eager_graph_eager first argument must be array of symbols"
 76   end
 77 
 78   current = model
 79   deps = dependency_chain.map do |dep|
 80     unless ref = current.association_reflection(dep)
 81       raise Error, "invalid association #{dep.inspect} for #{current.inspect}"
 82     end
 83     current = ref.associated_class
 84     [dep, ref.returns_array?]
 85   end
 86   assocs = current.dataset.send(:eager_options_for_associations, assocs)
 87 
 88   deps.each(&:freeze)
 89   deps.unshift(current)
 90   deps.freeze
 91 
 92   assocs.freeze
 93 
 94   if h = @opts[:eager_graph_eager]
 95     h = Hash[h]
 96     h[deps] = assocs
 97   else
 98     h = {deps => assocs}
 99   end
100 
101   clone(:eager_graph_eager=>h.freeze)
102 end

Protected Instance methods

eager_graph_build_associations(rows)

After building objects from the rows, if eager_graph_eager has been called on the datasets, for each dependency chain specified, eagerly load the appropriate associations.

[show source]
    # File lib/sequel/plugins/eager_graph_eager.rb
109 def eager_graph_build_associations(rows)
110   objects = super
111 
112   if eager_data = @opts[:eager_graph_eager]
113     eager_data.each do |deps, assocs|
114       current = objects
115 
116       last_class, *deps = deps
117       deps.each do |dep, is_multiple|
118         current_assocs = current.map(&:associations)
119 
120         if is_multiple
121           current = current_assocs.flat_map{|a| a[dep]}
122         else
123           current = current_assocs.map{|a| a[dep]}
124           current.compact!
125         end
126 
127         current.uniq!(&:object_id)
128       end
129 
130       last_class.dataset.send(:eager_load, current, assocs)
131     end
132   end
133 
134   objects
135 end