Module: Sequel::Plugins::EagerGraphEager::DatasetMethods

Defined in:
lib/sequel/plugins/eager_graph_eager.rb

Instance Method Summary collapse

Instance Method Details

#eager_graph_eager(dependency_chain, *assocs) ⇒ Object

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sequel/plugins/eager_graph_eager.rb', line 73

def eager_graph_eager(dependency_chain, *assocs)
  unless dependency_chain.is_a?(Array) && dependency_chain.all?{|s| s.is_a?(Symbol)} && !dependency_chain.empty?
    raise Error, "eager_graph_eager first argument must be array of symbols"
  end

  current = model
  deps = dependency_chain.map do |dep|
    unless ref = current.association_reflection(dep)
      raise Error, "invalid association #{dep.inspect} for #{current.inspect}"
    end
    current = ref.associated_class
    [dep, ref.returns_array?]
  end
  assocs = current.dataset.send(:eager_options_for_associations, assocs)

  deps.each(&:freeze)
  deps.unshift(current)
  deps.freeze

  assocs.freeze

  if h = @opts[:eager_graph_eager]
    h = Hash[h]
    h[deps] = assocs
  else
    h = {deps => assocs}
  end

  clone(:eager_graph_eager=>h.freeze)
end